HttpWebRequest on Compact Framework: The operation has timed-out

With .Net Compact Framework I developed an application that is doing a webrequest on interval bases. With the webrequest I sent data by providing some arguments inside the querystring. I am not interested in the response and therefore I did not assign the GetResponse() return value. I use the following code:

<br />
HttpWebRequest webRequest;</p>
<p>try<br />
{<br />
   webRequest = WebRequest.Create("http://someurl.com/with.aspx?querystring=value");<br />
   webRequest.Method = "GET";<br />
   webRequest.KeepAlive = false;<br />
   webRequest.GetResponse();<br />
}<br />
catch (Exception e){<br />
    //Something goes wrong. Implement handling of this exception<br />
}<br />
finally {<br />
   webRequest = null; //To clear up the webrequest<br />
}<br />

This code works… for only 2 times. The third time it is executed I get a: The operation has timed-out exception. It looks like the GetResponse() method keeps a connection (or socket) open which make it not possible to make new connection at the third time the method is executed. I solved this problem by assigning the GetResponse() value to a WebRequest variable and execute the Close() method inside the finally part of the try catch:

<br />
HttpWebRequest webRequest;<br />
WebResponse webResponse = null; //Need to assign this as null otherwise it does not compile<br />
try<br />
{<br />
   webRequest = WebRequest.Create("http://someurl.com/with.aspx?querystring=value");<br />
   webRequest.Method = "GET";<br />
   webRequest.KeepAlive = false;<br />
   webResponse = webRequest.GetResponse(); //Assign to webResponse<br />
}<br />
catch (Exception e){<br />
    //Something goes wrong. Implement handling of this exception<br />
}<br />
finally {<br />
   if(webResponse != null)<br />
   {<br />
      webResponse.Close(); //Close webresponse connection<br />
   }</p>
<p>   webResponse = null; //To clear up the webresponse<br />
   webRequest = null; //To clear up the webrequest<br />
}<br />

Read more from the Uncategorized category. If you would like to leave a comment, click here: 5 Comments. or stay up to date with this post via RSS, or you can Trackback from your site.

Command-line arguments Windows Mobile

In September I will go on holiday to the United States for 3 weeks. It would be nice to send updates to family and friends by placing images on Picasa. These web images will be taken with the photocamera from my mobile (which is Windows Mobile 6 based). I will upload the pictures when a free WiFi access point is available.

The program will place taken photos in a queue. When I have an internet connection available I can upload all the pictures from this queue to Picasa. This results in an application that can be seperated in two parts: the take a photo functionality and the upload photo-queue functionality.

Use cases explaining functionality

My mobile has a photo camera button which will trigger the default photo camera application. This default camera application can be replaced by changing the button settings on the mobile phone. When replaced (and when clicking the camera button) I want my application to start up ready to take a picture (so it will behave the same as the default camera application). After taking a picture you can type in some comment and add it to the queue.

When arriving at the hotel (or some other connective place) I want to upload the taken images. I can off course push the photo camera button again, but this time it is not my intention to take a new picture. In this case I want to start the application from the Start -> Programs menu and see the state of the queue with the option to upload the photos. The same application but with a different startup state.

The use of command-line arguments should solve this problem. But is this also possible with the Compact .Net Framework

Not a solution: Environment.GetCommandLineArgs

For Windows Form based application (which my mobile application is) the use of the Environment class is suggested to get the command-line arguments. The static method

<br />
public static string[] GetCommandLineArgs()<br />

The problem is that this method is not implemented inside the .Net Compact Framework and can’t therefore not been used.

Solution: static void Main(String[] args)

Just like console application (written in .Net) you have a Main method for your Windows Forms application. Besides the files created for the Form (by default Form1.cs and Form1.Designer.cs) a Program.cs file is created which contains a Main method. By adding a String[] args to the Main method, you can work with command-line arguments. An example of the new Main method

<br />
[MTAThread]<br />
static void Main(String[] args)<br />
   {<br />
      if (args != null &#038;&#038; args.Length > 0)<br />
     {<br />
         foreach (String arg in args)<br />
         {<br />
            MessageBox.Show(arg);<br />
         }<br />
     }<br />
     else<br />
     {<br />
         MessageBox.Show("No program arguments");<br />
     }<br />
     Application.Run(new Form1());<br />
}<br />

The arguments can be given to the form (Form1 in this example) by changing the constructor.

Creating a shortcut

The easiest way to create a shortcut is by using the Windows Explorer on your mobile phone. Navigate to the executable file of the application. Tap and hold the file selected with your styles to get the context menu. Select copy.

At the same location (or on a different location) tap (and hold) on an empty space to get the context menu. This context menu will give you the option to paste as a shortcut.

To add the command line argument inside the shortcut, i copied the shortcut (.lnk file) with ActiveSync to my desktop pc and edit the content with notepad. The file will now look like this:

59#”\Programmabestanden\ProgramArguments\ProgramArguments.exe” this_is_a_program_argument

My Windows Mobile OS is Dutch. On an English based OS the Programmabestanden part will be replaced by something like Program files. I don’t know if the number 59 before the line is mandatory. The number of characters with quotes is 59 long. So maybe it is used for reading optimalization. I really don’t know. I just added the this_is_a_program_argument at the end of the shortcut, saved the file and copied it back to my pocket pc.

Starting the application with the shortcut will show a messagebox with the text this_is_a_program_argument. When starting the application from the normal executable you will get a messagebox saving No program arguments.

Attachment: example project

You can download the example application from here

Special thanks

I want to thank Paul for discussing this problem and finding a solution.

Read more from the English, Research, Software development category. If you would like to leave a comment, click here: 2 Comments. or stay up to date with this post via RSS, or you can Trackback from your site.