Multiple Instances
-
I want to avoid, that the user can start multiple instances of my programm. I found an article on CodeProject which helped, but it's a little bit confusing now when the user tries to start the programm and in doesn't start. Therefore I want, that the second instance activates (setting the windowstate to normal, calling Show() and Activate() the first instance (when it's minized or minimized to tray) before it closes itself. How do I do that?
-
I want to avoid, that the user can start multiple instances of my programm. I found an article on CodeProject which helped, but it's a little bit confusing now when the user tries to start the programm and in doesn't start. Therefore I want, that the second instance activates (setting the windowstate to normal, calling Show() and Activate() the first instance (when it's minized or minimized to tray) before it closes itself. How do I do that?
Just answered this exact question a few pages down: RE: Singleton with a twist[^] Matt Gerrans
-
Just answered this exact question a few pages down: RE: Singleton with a twist[^] Matt Gerrans
-
What the hell... now it takes 1-2 seconds longer to start up. And btw. your code crashes. It seems to be that some proccess have a read protected process.MainModule Class. If I wrap around a try-catch it works. Thx for the answer
-
I want to avoid, that the user can start multiple instances of my programm. I found an article on CodeProject which helped, but it's a little bit confusing now when the user tries to start the programm and in doesn't start. Therefore I want, that the second instance activates (setting the windowstate to normal, calling Show() and Activate() the first instance (when it's minized or minimized to tray) before it closes itself. How do I do that?
You mentioned that your previous attempt didn't work. Were you using a mutex?
string uniqueid = Application.ExecutablePath.Replace(@"\", @"_"); Mutex m = new Mutex(false, uniqueid); if (m.WaitOne(1, true)) { // Application is not running so carrying on running } else { // Application is already running so end here }
-
You mentioned that your previous attempt didn't work. Were you using a mutex?
string uniqueid = Application.ExecutablePath.Replace(@"\", @"_"); Mutex m = new Mutex(false, uniqueid); if (m.WaitOne(1, true)) { // Application is not running so carrying on running } else { // Application is already running so end here }
-
What the hell... now it takes 1-2 seconds longer to start up. And btw. your code crashes. It seems to be that some proccess have a read protected process.MainModule Class. If I wrap around a try-catch it works. Thx for the answer
> What the hell... now it takes 1-2 seconds longer to start up. And btw. your code crashes. It seems to be that some proccess have a read protected process.MainModule Class. If I wrap around a try-catch it works. Well, don't worry, there's a full money-back guarantee. Just send one proof-of-purchase and a self addressed, stamped envelope and you'll get refunded the full amount. If your startup time increased, that's probably because of something else you did. It doesn't take that long to create a mutex. My app starts up in less than a second, so that couldn't be it. Post your profiler logs and I'll believe you. Anyway, next time, I'll be sure and submit everything to a full QA cycle, running it through an exhaustive matrix of OS (including all service packs and languages, of course), hardware and environment configurations before posting any suggestions. Did you miss the "you've got the gist of it and can proceed with the fine-tuning as you like" part? If you expect people to fully vet any suggestions, then you might perhaps state how much you are willing to pay for that work. Matt Gerrans
-
Seems to be a bug in
System.Diagnostics.Process
that theMainWindowHandle
is0
when the window is hidden. I wonder if the assumption they are making is that if the main window is hidden, it should be treated like a "daemon" or "service" kind of app (and thus it pretends that there is no main window). Anyway, I guess if you are hiding your window, then you could useFindWindow()
orEnumWindows()
to get the handle instead ofSystem.Diagnostics.Process.GetProcesses()
. There are other ways I can think of too... Matt Gerrans -
> What the hell... now it takes 1-2 seconds longer to start up. And btw. your code crashes. It seems to be that some proccess have a read protected process.MainModule Class. If I wrap around a try-catch it works. Well, don't worry, there's a full money-back guarantee. Just send one proof-of-purchase and a self addressed, stamped envelope and you'll get refunded the full amount. If your startup time increased, that's probably because of something else you did. It doesn't take that long to create a mutex. My app starts up in less than a second, so that couldn't be it. Post your profiler logs and I'll believe you. Anyway, next time, I'll be sure and submit everything to a full QA cycle, running it through an exhaustive matrix of OS (including all service packs and languages, of course), hardware and environment configurations before posting any suggestions. Did you miss the "you've got the gist of it and can proceed with the fine-tuning as you like" part? If you expect people to fully vet any suggestions, then you might perhaps state how much you are willing to pay for that work. Matt Gerrans
-
I want to avoid, that the user can start multiple instances of my programm. I found an article on CodeProject which helped, but it's a little bit confusing now when the user tries to start the programm and in doesn't start. Therefore I want, that the second instance activates (setting the windowstate to normal, calling Show() and Activate() the first instance (when it's minized or minimized to tray) before it closes itself. How do I do that?
-
I'm sorry if my comments sound to offensive/overacting. I alway speak a little bit to rough, I'll try better next time :)
Glad to hear it. And glad to hear (in your other post) that you got it all working smoothly. Matt Gerrans