Adding an On-Demand Programmatic Update

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies.
This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
To create client business applications using current Microsoft technologies, see patterns & practices' Prism.

This topic describes how to use the ClickOnce programmatic API to control when an update will be applied on a deployed application.

Prerequisites

Before you can complete the steps in this topic, you must complete the following tasks:

  • Make a copy of the ReferenceImpl folder. You will be modifying the Bank Branch Workbench reference implementation in this procedure. To avoid having to unzip the Smart Client Software Factory again to restore the original version, we recommend that you make a copy of the reference implementation solution folder now.
  • Use the Control Panel Programs option to uninstall all previous versions of the GlobalBank.Shell application that were installed through ClickOnce.
  • Remove previous publications from the deployment server. To do this, remove all contents from the Publish folder (C:\Inetpub\wwwroot\GlobalBank).
  • Remove previous publications from the local folder used to stage the deployments before they are moved to the Web server (C:\_Publish\SCSF).

Steps

In this procedure, you will do the following:

  1. Configure ClickOnce update settings by turning off automatic updating and supplying an update location URL.
  2. Add code to the application to perform on-demand updates.
  3. Test the update process.

Step 1: Configuring ClickOnce Update Settings

To configure ClickOnce update settings

  1. In the Shell application, double-click Properties to show the Property pages, and then click the Publish tab.

  2. Click Updates.

  3. Clear the The application should check for updates check box.

  4. In the Update Location text box at the bottom of the dialog box, type the update URL (https://localhost/GlobalBank/) as shown in Figure 1.

    Ff699336.e0d16951-3545-4986-bfa3-e3b14fb5553f(en-us,PandP.10).png

    Figure 1

    Application Updates dialog box

    Note

    If you disable update checking, you must explicitly set the update location; otherwise, the deployment provider URL will not be set in the deployment manifest and on-demand updates will fail. However, you could manually set the deployment provider URL through the Manifest Manager Utility or Mage.

  5. Click OK.

  6. In the Publish Version text boxes, set the version to 1.0.0.0.

  7. Clear the Automatically increment revision with each publish check box.

    Your Publish properties should look like Figure 2.

    Ff699336.f9c907b7-2220-4f74-b455-23d89e3400ee(en-us,PandP.10).png

    Figure 2

    Publish properties

  8. Click Publish Now.

Step 2: Adding Code to Perform On-Demand Updates

To add timed on-demand update checking to the application

  1. Run Visual Studio as an administrator.

  2. In the Designer, open ShellForm.cs.

  3. Move a timer from the Toolbox onto the form.

  4. In the Properties window of the timer, set the Interval property to 60000 (milliseconds) to check for updates every minute.

  5. In the Properties window, go to the Events view.

  6. Add an event handler named OnCheckForUpdates for the Tick event.

  7. In the Code view, add using statements for the System.Deployment.Application and System.ComponentModel namespaces, as shown in the following example.

    using System.Deployment.Application;
    using System.ComponentModel;
    
  8. Modify the constructor to enable the timer (that is, start it ticking) and hook up event handlers if the application has been launched through ClickOnce, as shown in the following example.

    public ShellForm()
    {
       InitializeComponent();
    
       _layoutWorkspace.Name = WorkspaceNames.LayoutWorkspace;
       // Check to see whether you are running through a ClickOnce launch.
       if (ApplicationDeployment.IsNetworkDeployed)
       {
          timer1.Enabled = true;
          // Hook up an event handler for update check completed events.
          ApplicationDeployment.CurrentDeployment.CheckForUpdateCompleted
             += OnCheckForUpdatesCompleted;
          ApplicationDeployment.CurrentDeployment.UpdateCompleted
             += new AsyncCompletedEventHandler(OnUpdateCompleted);
       }
    }
    
  9. Add the following code to the event handler for the Tick event.

    private void OnCheckForUpdates(object sender, System.EventArgs e)
    {
       // Check to ensure the application is running through ClickOnce.
       if (ApplicationDeployment.IsNetworkDeployed)
       {
          // Check for updates asynchronization.
          ApplicationDeployment.CurrentDeployment.CheckForUpdateAsync();
       }
    }
    
  10. Add the following Boolean flag member to the form.

          bool m_RequiredUpdateDetected = false;
    
  11. Add the following event handler for the CheckForUpdateCompleted event.

    void OnCheckForUpdatesCompleted(object sender,
       CheckForUpdateCompletedEventArgs e)
    {
       if (e.UpdateAvailable)
       {
          if (e.IsUpdateRequired)
          {
             m_RequiredUpdateDetected = true;
          }
          ApplicationDeployment.CurrentDeployment.UpdateAsync();
       }
    }
    
  12. Add the following event handler for the UpdateCompleted event.

    void OnUpdateCompleted(object sender, AsyncCompletedEventArgs e)
    {
       if (m_RequiredUpdateDetected)
       {
          MessageBox.Show("Required update downloaded, application will restart");
          Application.Restart();
       }
       else
       {
          DialogResult result = MessageBox.Show(
             "Application update downloaded. Would you like to restart?", 
             "Global Bank", MessageBoxButtons.YesNo);
          if (result == DialogResult.Yes)
             Application.Restart();
       }
    }
    

Step 3: Testing the Update Process

To test the update process

  1. Publish an initial version of the Bank Branch client application with the on-demand update code in it. Use the procedure in How to: Publish an Initial Version of an Application.
  2. Launch the initial version of the Bank Branch client application. Use the procedure in How to: Deploy an Initial Version of an Application.
  3. Close the application so that it is not checking for updates while you are publishing the new version.
  4. Make a simple but noticeable update to the Shell application. An easy example is to modify the title bar text (Text property) for the ShellForm form.
  5. In the Shell application, double-click Properties to display the Property pages, and then click the Publish tab.
  6. In the Publish Version text boxes, set the version to 1.1.0.0.
  7. Publish the updated version. Use the procedure in How to: Publish an Updated Version of an Application.
  8. Use the Start menu to launch the client application. After about a minute, it should detect and download the update, and then prompt you to restart.

Outcome

Your application checks for updates in the background based on a timer interval that you control. When updates are detected, they are immediately downloaded and the user is prompted to restart the application.