Single instance app - what's the beswt way?

  • Thread starter Thread starter Brian W
  • Start date Start date
B

Brian W

Hi all,

I'm working on a Windows App and am making it single-instance app, that part
I have working like this:

static void Main()
{
try
{
System.Threading.Mutex mutex = new System.Threading.Mutex(true,
"2F8F656D-E6E8-4646-A529-DB3D67279F6F.MyNew.Application");
if ( !mutex.WaitOne(1, true) )
{
return;
}
}
catch
{
return;
}
Application.Run(new MainWindow());
}
}

The part I'm having trouble with is activating the existing Window (Bring to
front, Restore, etc).

In the "old days" I would code my Window so it had a unique window class
name and use FindWindow blah blah blah

-- or --

I would make my app a COM server and invoke methods on COM object that would
in turn do the necessary work on the main window.

What is the best approach "these days" with .NET and the framework?

TIA
Brian W
 
Have you seen Chris Sell's Windows Forms book? He has a bunch of good info
on this topic.
 
Hi Brian,

In .Net, you also can P/invoke these win32 api functions.
Then you can do like in Win32 application.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Sure, of course I can use P/invoke and do it the "old" way, I was looking
for a more ".NET way" to do it.
 
Hi Brian,

Actually, the .Net class library still did not fully cover all the windows
features, there are no .Net classes in library to get this done.
I think the only way to get this done is P/invoke.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top