Updating your application via the Internet |
||
Submitted on November 1, 2004 Currently the newer version of the Clever Internet Suite 6.2 is available. This version includes the Web Update component. Also please check the How to migrate to the Clever Internet Suite Version 5.0 article. IntroductionIn this article we continue discussing the task of receiving updates from the Internet. Starting from the moment when the first part of this article was published (Adding automatic update support to your application, 25 March 2003) we received many comments and requests from our readers about extending the main functionality of the WebUpdate Delphi component. After collecting and analyzing all these feature requests, we have selected the most important of them and decided to publish new WebUpdate component. The most frequently asked features for the Internet updater tools can be displayed in the following order:
Let us move ahead and discuss all of these features in details. Receiving updates in a backgroundThis option is necessary when the update files are large and it is important to do not interrupt the main application process while receiving the updates. In order to satisfy this condition we have used the TclDownloader component from the Internet Components - Clever Internet Suite library. Generally speaking it is possible to use any other library you prefer but since the TclDownloader component supports the asynchronous downloading mode out of the box we will use it in our examples extensively. If your library does not support the asynchronous downloading mode, you have to implement a thread inside of which your socket control will perform the downloading process. When the update retrieving is started, the StartDownloading method is called and returns immediately since the whole downloading process is performed inside of internal TclDownloader's thread.
The ProcessCompleted method is called automatically when the downloader has finished its work.
Receiving of multiple non-cumulative updatesThe algorithm of receiving of updates from the Web is described in the Adding automatic update support to your application article in details. The main difference between two these algorithms is that the new algorithm allows you to receive multiple updates at the same session. This is necessary requirement if you run your application periodically and there are more than one Web updates are coming between two sessions of your work. In such case you must keep the information about all previously downloaded updates in order to do not download them again. In the WebUpdate component we have used the XML document object model for storing of such information. The listing below demonstrates the comparing process of the updates available from the Web and ones which were already downloaded during the previous sessions.
Updating of the main application executableAfter all updates are received, they are executed using the corresponding information from their Command Batch scripts. You can associate the command batch statements (such as copy, unzip, del...) with each update archive separately. When the WebUpdate component processes the downloaded updates, all updates are applied immediately except for ones which contain the main application executable. Such updates have the NeedTerminate flag enabled. In order to update the main application executable and also all locked resources the user must have a chance to terminate the application. If at least one of the update files has the NeedTerminate flag enabled, the OnTerminating event is raised just after the last received update is processed. It is important to accumulate the updates which require the main application to be closed. The implementation of this algorithm is application-dependant. One of the possible ways is to store the updates command batch scripts into the separated command batch file (starter.cmd, e.g). The last statement of the starter.cmd file should always contain the line which runs the main application executable. Please Note! You must always run your application using this starter.cmd file and not using the application executable. Next time you run your application, the executable file will be replaced with new one and finally will be started. Another way you can use the separated application for the WebUpdate component. In this article we do not consider all possible implementations of the updating of locked files. This is a place where you can realize your own best solutions.
The event handler for the OnRunUpdate component event looks like the following:
Downloading large-size updates with the resuming of the broken / interrupted downloadsThe Microsoft Windows Updater works in a background and continues the downloading process through sessions of working or from time to time the PC is connected to the Internet. In order to continue the downloading process from the point where it is stopped you have to store the current position within the file being downloaded and start the downloading process at this point next time you continue working with your application. Since TclDownloader gets the web resources in the Multiple Threads simultaneously, you must store the current positions for each of these threads. You can learn more about this mode at Multipart Multithreaded Downloading page. The listing below demonstrates how to store and load the multipart downloading state into XML:
Managing of the updates available on the serverAs it is described in Adding automatic update support to your application, all updates available on the server are managed by the XML document which is also available on the server. In order to simplify the organizing of such XML document and keeping it up-to-date, we have developed simple program. This program allows you add the information about new updates and also modify existing ones with the user-friendly interface and do not bother about the xml document structure. All sources of the Delphi project for this application together with compiled executable can be downloaded at WebUpdateSrc.zip Source Code and working sampleA full source code of all classes, UpdateManager program and also the test application described in this article can be downloaded at WebUpdateSrc.zip What the test application does:
The updated version of the TestApplication.exe file will contain the ' - Updated' phrase within the application window caption. This code is constantly being refined and improved and your comments and suggestions are always welcome. With best regards, |