![]() |
Languages »
C# »
How To
Intermediate
Using Updater BlockBy Praveen NayakA vanilla implementaion showing how Updater Block 2.0 can be included in a Windows Forms application. |
C#.NET 1.1, Win2K, WinXP, Win2003IIS 5.1, IIS 6, VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
I had been trying to include updater block in my application. The quick starts that came along with the application block had some real neat examples, but it took quite some time to figure out what files must be placed where, what settings needed to be made and how, to get a sample of my own working. So, I thought I would post the sample implementation that I generated, along with a step by step guide so that others who will be trying this point onwards need not struggle much.
Note: A good tutorial for updater block version 1.0 can be found here.
The example below tries to show the usage for the version 2.0 of the Updater block.
SampleForm and set its text to �Version 1.0.0.0�
Application.Exit();.
eventList.
Main () body to Application.Run(new SampleForm());.
Note: The downloadable source has this content, but what the heck.
SampleForm Class to start off the updater manager: InitializeAutoUpdater ();ApplicationID: SampleApplication.
BasePath: C:\Temp\UpdaterTest\UAB.
ManifestUri: http://localhost/ApplicationBlockServer/ServerManifest.xml.
Note: The ManifestUri will point to a URL that has to be created later.
The server application will be a newer version of the application deployed at c:\temp\ UpdaterTest.
SampleForm in designer mode and make noticeable changes on the form. Change the label text to �Version 1.0.0.1� and the form text to �Sample Form New Version�.
The new version of the application will be deployed at the server, from where it can be downloaded.
The application, after some time, will pops up a dialog box telling that a newer version is available and do you want to download it. Click yes. The newer version will be downloaded which will be displayed in the list box eventList. After downloading, it will prompt you to restart the application. Close the application and start it again. The version 1.0.0.1 will open up.
Addendum:
Hey, this was just a vanilla implementation.
Auto Update Code to be pasted in the application
#region Auto-Update Stuff
private Thread pollThread = null;
private int manifestDownloaded = 0;
private void CheckAndUpdate()
{
try
{
// Get the updater manager
ApplicationUpdaterManager updater =
ApplicationUpdaterManager.GetUpdater();
// Subscribe for various events
updater.DownloadStarted +=
new DownloadStartedEventHandler(updater_DownloadStarted);
updater.DownloadProgress +=
new DownloadProgressEventHandler(updater_DownloadProgress);
updater.DownloadCompleted +=
new DownloadCompletedEventHandler(updater_DownloadCompleted);
updater.DownloadError +=
new DownloadErrorEventHandler(updater_DownloadError);
updater.ActivationInitializing +=
new ActivationInitializingEventHandler(updater_ActivationInitializing);
updater.ActivationStarted +=
new ActivationStartedEventHandler(updater_ActivationStarted);
updater.ActivationInitializationAborted +=
new ActivationInitializationAbortedEventHandler(
updater_ActivationInitializationAborted);
updater.ActivationError +=
new ActivationErrorEventHandler(updater_ActivationError);
updater.ActivationCompleted +=
new ActivationCompletedEventHandler(updater_ActivationCompleted);
// Loop till the updates are available
Manifest[] manifests = null;
while(true)
{
manifests = updater.CheckForUpdates();
if(manifests.Length > 0)
{
// Prompt user if he wants to apply the updates
if( MessageBox.Show(this,
"Update for Auto Inproc Application is available,"+
" do you want to apply the update?",
"Update",MessageBoxButtons.YesNo)== DialogResult.Yes)
{
foreach(Manifest m in manifests)
{
m.Apply = true;
}
// update the application as per manifest details.
updater.Download( manifests, TimeSpan.MaxValue );
if(manifestDownloaded == manifests.Length)
{
updater.Activate( manifests );
manifestDownloaded = 0;
}
break;
}
else
{
Thread.Sleep(10000);
}
}
else
{
Thread.Sleep(10000);
}
}
}
catch(ThreadAbortException)
{
// Do nothing if the thread is being aborted,
//as we are explicitly doing it
}
catch(Exception ex)
{
MessageBox.Show(this,ex.Message,"Error",
MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
private void exitButton_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void updater_DownloadStarted(object sender,
DownloadStartedEventArgs e)
{
UpdateList("DownloadStarted for manifest: " + e.Manifest.ManifestId);
}
private void updater_DownloadProgress(object sender,
DownloadProgressEventArgs e)
{
UpdateList("DownloadProgress for manifest: "+ e.Manifest.ManifestId +
"- Files: "+e.FilesTransferred+"/"+e.FilesTotal+
" - Bytes: "+e.BytesTransferred+"/"+e.BytesTotal);
}
private void updater_DownloadCompleted(object sender,
ManifestEventArgs e)
{
UpdateList("DownloadCompleted for manifest: " +
e.Manifest.ManifestId);
manifestDownloaded++;
}
private void updater_DownloadError(object sender,
ManifestErrorEventArgs e)
{
UpdateList("DownloadError for manifest: " +
e.Manifest.ManifestId +"\n"+e.Exception.Message);
}
private void updater_ActivationInitializing(object sender,
ManifestEventArgs e)
{
UpdateList("ActivationInitializing for manifest: " +
e.Manifest.ManifestId);
}
private void updater_ActivationStarted(object sender, ManifestEventArgs e)
{
UpdateList("ActivationStarted for manifest: " + e.Manifest.ManifestId);
}
private void updater_ActivationInitializationAborted(object sender,
ManifestEventArgs e)
{
UpdateList("ActivationInitializationAborted for manifest: " +
e.Manifest.ManifestId);
MessageBox.Show(this,
"The Application needs to restart for applying the updates," +
" please restart the application.",
"Auto Inproc Updates",MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
private void updater_ActivationError(object sender,
ManifestErrorEventArgs e)
{
UpdateList("ActivationError for manifest: " + e.Manifest.ManifestId +
"\n"+e.Exception.Message);
}
private void updater_ActivationCompleted(object sender,
ActivationCompleteEventArgs e)
{
UpdateList("ActivationCompleted for manifest: " + e.Manifest.ManifestId);
}
private void UpdateList(string displayString)
{
eventList.Items.Add(displayString);
eventList.Update();
}
private void InitializeAutoUpdater ()
{
// Seperate thread is spun to keep polling for updates
ThreadStart checkUpdateThreadStart = new ThreadStart(CheckAndUpdate);
pollThread = new Thread(checkUpdateThreadStart);
pollThread.Start();
}
#endregion
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 12 Apr 2005 Editor: Sumalatha K.R. |
Copyright 2005 by Praveen Nayak Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |