简体   繁体   中英

Allow only a single windows messagebox for single instance application

I have a small Windows application which is allowed to run only once. The single instance check is done using a Windows mutex (CreateMutex).

The application should bring the already running application to the front and show an info message to the user.

The info message is created using the MessageBox function from Windows. However, each time I try to start a new instance of my application, a new messagebox is created (allowing me to open hundreds of messageboxes).

Is there a way to limit the number of message boxes to one (besides locking another mutex for the message box)?

There are three states of your program:

  1. Normal (first instance),
  2. Informing about second run (second instance, showing message box about it right now),
  3. Extra (more than second instance, which knows about showed message box).

It seems reasanoble for the program in third state to quit silently. There are many ways to do it. And since you are already using mutex for detection of second state, is will be reasanoble to use the same logic for detection of third state too.

But you can do it another way:

  1. Second instance of your program can inform first instance of the program about second run and quit.
  2. And main (first) instance of the program will show the message box after receiving of this kind of message. In this case your program should ignore all such messages begore closing of current message box (to avoid showing of hungreds of such message boxes one by one).

From my point of view, first approach is better (ie to show message box by second/third/... instance of the program, and only after it to bring main instance to the front).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM