Groups | Blog | Home
all groups > dotnet windows forms > may 2005 >

dotnet windows forms : Best way to implement a C# Windows Forms app. as a Singleton?


None
5/28/2005 9:54:32 PM
I only want one instance of my C# Windows Forms app ever to run at the same
time.

I'm familiar with the Singleton pattern of course. What's the best way to
achieve that pattern with a Windows Forms app?

Jon Skeet [C# MVP]
5/29/2005 12:00:00 AM
[quoted text, click to view]

See http://www.pobox.com/~skeet/csharp/faq/#one.application.instance

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
None
5/29/2005 10:22:10 PM
How about using Process.GetProcessesByName? Any drawbacks or gotchas to
that? I've tried it and it seems to work but I'm worried about unforeseen
complications or problems.


[quoted text, click to view]

Jon Skeet [C# MVP]
5/30/2005 12:00:00 AM
[quoted text, click to view]

The most obvious gotcha is that two processes could have the same name
but be completely unrelated.

I seem to remember it also takes a while to complete, although I could
be wrong.

It just feels a bit messy to use names which already have one meaning
when you effectively want a system wide locking object, which is
exactly what a Mutex is.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
AddThis Social Bookmark Button