2008年5月21日星期三

Ensuring Only One Instance of Your Application Is Running -- 确保只有一个程序实例运行

Many applications must ensure that they have only one instance running. There are several ways to do this in Windows XP. Among them are the following:
许多程序都要确保只能运行一个实例。这个有多个实现方法。总的有以下:

→Use FindWindow or FindWindowEx to search for a known window that your application opens. If that window is already open, you can use that as an indication that the application is already running.
使用FindWindow或者FindWindowEx去查找你的应用程序窗口,如果窗口打开了,就说明程序已经在运行。

→Create a mutex or semaphore object when your application is opened, and close that object when the application terminates. The global object namespace is separated for each desktop, allowing a unique list of mutex and semaphore objects for each.
当你的应用程序打开时创建一个互斥或者信号对象,在程序结束时关闭这个对象。这个全局的对象名字空间在每个桌面是分开的,每个桌面允许有一系统的唯一的互斥和信号对象。

[Reference from MSDN-User Accounts with Fast User Switching and Remote DesktopUser Accounts with Fast User Switching and Remote Desktop]

没有评论: