Main Photo Album Fair Tail Programming GuestBook | Russian |
How To Create Restartable Application
IntroductionIn 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 codeUsing 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 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 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 refecrenceMacro#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:
You can redefine mutex name for your purposes. FunctonsBOOL 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 DownloadsDemo projectDemo 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 FilesSource files include "RestartAPI.h" and "RestartAPI.cpp" which can be used separatly in your Win32 application Top of Page Programming |
ICQ UIN:63179813
(Add to contact list,
Message for Me) e-mail: Alexander D. Alexeev |
© 2002-2006 Alexander D. Alexeev |