Here's a quick and dirty snippet (and sample usage) to check if your app is already running (using a Mutex).
static class Program
{
private static Mutex mutex;
public static bool IsThisProcessAlreadyRunning()
{
bool createdNew;
mutex = new Mutex(false, Application.ProductName, out createdNew);
return !createdNew;
}
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
if (!IsThisProcessAlreadyRunning())
{
// whatever you might be doing
// here's some default form initialization
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
else
{
// do something!
// usually smt like --> BringOnFront();
}
}
}
Bringing the app to the foreground is usually what you'll want to do next in scenarios like this. A post about that might follow soon enough.
Side note - if you're using VB.NET I believe there is a check-box you can tick under application properties somewhere that does the job. I'll avoid making jokes about VB.NET 'cause life is tough enough for the suckers who use it without me being a jerk.
EDIT: 2009-11-01 --> posted How to Bring Window Upfront
No comments:
Post a Comment