Click here to Skip to main content
15,881,380 members
Articles / Programming Languages / C# 5.0

Restart Manager Support For Windows Application

Rate me:
Please Sign up or sign in to vote.
4.75/5 (8 votes)
16 May 2014CPOL4 min read 27.8K   554   25   1
Restart Manager Support For Windows Application

Introduction

I was surfing internet for good article on how to add Restart Manager support for a Windows Form based application developed in C#.NET for Windows VISTA and higher operating system, I could not find any good one so decided write one myself.

Restart Manager

Its a feature provided by Microsoft which allows users to update application software that is running in user system safely. The Restart Manager was introduced with Windows Vista and Windows Server 2008 OS.

The Restart Manager provides a set of APIs which can be used by a Windows Installer version 4.0 or higher and application that it intends to upgrade safely without loosing any critical work its performing.

Before Restart Manager came into picture if user had to update a running application then he has to manually stop the application, run update installer, start application once again. This sounds simple enough, but if installer updates a suite of applications\services then user had to stop and start each of them.

Restart Manager over comes above problem by transferring responsibility from user to applications themselves to restart when their updated. Using of Restart Manager also reduces the number of restart required if multiple applications are being updated.

This article is mainly focused on making a C#.NET Windows form application to be Restart Manager aware allowing a Windows Installer to restart it, for more details on how to create the installer refer MSDN topic Using Restart Manager.

Making WinForm application Restart Manager Aware

Its three step process described in detail below:

Step 1: Register your application

To allow Restart Manager to restart application it must be registered with OS. This done by calling RegisterApplicationRestart method provided in kernel32.dll. Since our code is managed we need to import the native method.

C#
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern uint RegisterApplicationRestart(string pszCommandline, int dwFlags);  

In example program above attached I have wrapped the native method in RestartManagerWrapper class.

Now we can register our application with OS, usually done in constructor method.

C#
public MainApplicationForm()
{
    InitializeComponent();

    //Regsiter your application for restart
    if (RestartManagerWrapper.IsRestartManagerSupported)
    {
        string commandLine = null;

        RestartManagerWrapper.RegisterApplicationRestart(commandLine, ApplicationRestartFlags.NONE);

    }//End-if (RestartManagerWrapper.IsRestartManagerSupported)        
} 

Step 2: Save application state by listening to application restart message

If application needs to save it current state so that it can restart without losing any critical work then we need to listen to restart message. This is done by overriding WndProc in Form class.

C#
protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);

    //Check for shutdown message from windows
    if (m.Msg == WindowsMessages.WM_QUERYENDSESSION ||
        m.Msg == WindowsMessages.WM_ENDSESSION)
    {
         if (m.LParam.ToInt32() == LParameter.ENDSESSION_CLOSEAPP)
         {
           //Save application state so it can be restored when its restarted.

         }//End-if (m.LParam.ToInt32() == LParameter.ENDSESSION_CLOSEAPP)

    }//End-if (m.Msg == WindowsMessages.WM_QUERYENDSESSION ||
         //    m.Msg == WindowsMessages.WM_ENDSESSION)
 }

Step 3: Restore application state (Optional)

Once application has restarted then it may have to go state where it had been when it was shutdown. This may have to be done based on information saved in Step2 and we can know if it was restarted via Restart Manager by passing required parameters in Step1 while registering application.

Testing Restart Manager aware application

To test whether your application will restart as needed you can use Logo Testing Tools for Windows provided by Microsoft, it can be download via this link.

Once you have installed testing tool then you need to open command prompt and go to C:\Program Files\Microsoft Corporation\Logo Testing Tools for Windows\Restart Manager now based on your processor architecture you need to choose AMD64 or x86. My machine is x86.

Command Prompt

Now start your test application and get PID(Process Identifier) from Task Manager. Type the following command in above command prompt.

>RMTool.exe -p 682408 -S -R

Command Parameter explained below:
  • -p takes PID to know which application instance needs to restarted. The single character space must be provided between -p and PID.
  • -S indicates that application needs to be shutdown.
  • -R indicated that application needs to restarted after shutdown.

The RMTool.exe will initiate shutdown and restart sequence to specified application and will show result of each step as shown below.

Image 2
If an error occurs it will be shown to user as below. Based on which stage of shutdown or restart error occurs you may need to fix your application.

Image 3

Points of Interest

Encountered Issues :

  1. When I was adding Restart Manager support for one of my C#.NET application, I had to close all BITS jobs created by my application in main form's FormClosed event before terminating. When Restart Manager tried to shutdown my application it threw exception with following error message 'An outgoing call cannot be made since the application is dispatching an input-synchronous call. (Exception from HRESULT: 0x8001010D (RPC_E_CANTCALLOUT_ININPUTSYNCCALL)).'
    it looks like we cannot access COM services while shutdown occurs through Restart Manager, since same code works fine when user closes the application. If anybody has a solution this problem please contact me.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Gautham Prabhu K is a Software Engineer with 10+ years of experience in designing and developing software solution. He has experience working in Payment Gateway, Process Automation and Investment Banking industries.

Gautham has passion for engineering software solutions. He likes to design and build them efficiently, he has primarily worked on Microsoft .NET technology.

Gautham has dream of building his own software company someday. During his free time he likes to watch movies, go on long drives and write technical blogs/article.

Comments and Discussions

 
Questionnice article Pin
Darshan.Pa16-May-14 3:18
professionalDarshan.Pa16-May-14 3:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.