Main Photo Album Fair Tail Programming GuestBook RussianRussian

How To Create Restartable Application

Application Before Restart Application While Restarting Application After Restart
Step 1. Application Before Restart Step 2. Application While Restarting Step 3. Application After Restart

Introduction

In some cases your application must be restarted to apply some changes. For example you can download newer version of files or change UI language of application. For user is very convenient do not restart application manualy. The best way is to ask user for restart application now or later.

In this article I describe easy way to complete this task with a few lines of code.

Using the code

Using the restart code is very simple.

First include "RestartAPI.h" in your "stdafx.h" and add "RestartAPI.cpp" in your VC project.

Second step is modifying your main or WinMain function like shown below. Another way is to add restart initialize code to your application Start point and add restart finish to Exit point, such as PreMessageLoop/PostMessageLoop in WTL Application. Here is example of modified WinMain function

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{
	// Initialize restart code
	// Check if this instance is restarted and 
	// wait while previos instance finish
	if (RA_CheckForRestartProcessStart())
		RA_WaitForPreviousProcessFinish();
	//
	//
	// Your WinMain Code here
	//
	//
	// Finish restarting process if needed
	RA_DoRestartProcessFinish();
	return 0;
}

Third step is detrmine point where you want initialize restarting of application. In attached sample this point is button "Restart Me!" click handler. For initialize restarting of application you must call RA_ActivateRestartProcess function and terminate your application

LRESULT CMainDlg::OnBnClickedRestart(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	// Initialize restart proceess
	if (!RA_ActivateRestartProcess())
	{
		::MessageBox(NULL, _T("Something Wrong"), _T("Restart App"), MB_OK|MB_ICONEXCLAMATION);
		return 0;
	}
	// Terminate application.
	CloseDialog(IDC_RESTART);
	return 0;
}

Short functions refecrence

Macro

#define RA_CMDLINE_RESTART_PROCESS	TEXT("--Restart")

Command line switch for restarted application

You can redefine command line switch for your purposes. Restarted application started with this switch

#define RA_MUTEX_OTHER_RESTARTING	TEXT("YOUR_RESTART-MUTEX_NAME")

Mutex unique name

Named mutex is used for waiting termination of first instance of process. Mutex name must be unique, in that reason you can use GUID to define mutex name like this: #define RA_MUTEX_OTHER_RESTARTING TEXT("APPRESTART-E476AE82-AA92-11DA-ACE4-006098EFC07C")

You can redefine mutex name for your purposes.

Functons

BOOL RA_CheckProcessWasRestarted();

Return TRUE if process was restarted

BOOL RA_CheckForRestartProcessStart();

Check process command line for restart switch. Call this function to check that is restarted instance

BOOL RA_WaitForPreviousProcessFinish();

Wait till previous instance of process finished.

BOOL RA_DoRestartProcessFinish();

Call it when process finish

BOOL RA_ActivateRestartProcess();

Call this function when you need restart application. Start another copy of process with command line RA_CMDLINE_RESTART_PROCESS. After call RA_ActivateRestartProcess you must close an active instance of your application

Downloads

Demo project

Demo project includes sample WTL dialog application, that demonstrate how to use restarting API. At screenshot you can see some stages of application work.

At first screen application started normaly

At second screen user clicked "Restart Me!" button and you see pseudo termination progress. If you run task manager at this moment you'll see two instances of RestartableApp.exe

At last screen application in normal state after it was restarted

Source Files

Source files include "RestartAPI.h" and "RestartAPI.cpp" which can be used separatly in your Win32 application


Top of Page
Programming


ICQ UIN:my icq63179813 (Add to contact list, Message for Me)

e-mail:  Alexander D. Alexeev

© 2002-2006 Alexander D. Alexeev
Hosted by uCoz