Handling Unreliable Connections

The Internet is not the most reliable of places: connections can be instantly dropped for no apparent reason, and connections that before were blindingly fast can drop to a mind-numbingly slow crawl. Add to this the size of files that are transferred in today’s computing environments (Microsoft Word files with even a few embedded bitmaps can easily exceed one megabyte in size), and you have a very unstable environment for transferring files.

Because the OpenURL method is synchronous, execution of your code will not continue until the remote computer has processed the call. If the connection is disrupted or the remote computer is taking a long time to process the request, your code will not resume executing, effectively freezing your program. This can lead to very frustrated users.

To prevent this from occurring, you can set a maximum time (in seconds) for the Internet Transfer Control to wait for a response. The RequestTimeOut property is used for this purpose. If no data is received from the remote computer in this period, a time-out error will be generated in the StateChanged event. (The StateChanged event is explained later in this chapter.) An error message can then be displayed informing the user that because of a problem with the Internet connection, the file cannot be retrieved at this time (neatly avoiding any criticism of your code).