detecting instances of my App

G

Guest

I want to allow running only one instance of my Application.

How can I detect that a previous instance of my WinForm App is running in
order to stop the second instance?
 
G

Guest

hi thks

I was playing aroung with the code on the link but it works only when the
first application is run inside the VS.

But it does not works when the application is running twice outside the VS.

Also I'm using a VB winForm App

so I'm still looking..
 
J

Jon Skeet [C# MVP]

Kenny M. said:
I was playing aroung with the code on the link but it works only when the
first application is run inside the VS.

But it does not works when the application is running twice outside the VS.

Also I'm using a VB winForm App

so I'm still looking..

Did you read the whole of the section pointed at in the link, including
the bit which says:

<quote>
One thing to beware of is that the mutex isn't garbage collected. If a
local variable is only used near the start of a method, the GC may
ignore it when working out which variables are garbage collection
"roots" if that part of the method has already been executed. This can
lead to the mutex being released earlier than you might anticipate! To
prevent this from happening, make a call to GC.KeepAlive (mutex); at
the end of your main method. Alternatively, use a static variable to
store the mutex. That will ensure that the mutex is not garbage
collected until the AppDomain is unloaded. (That way even if Main
terminates, you won't have any problems if you've got other threads
running.)
</quote>

That's likely to be your problem. If not, could you post a short but
complete program which demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top