Sunday, April 25, 2010

Creating Single Instance of Your Application

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;

namespace myappliaction
{
static class Program
{
///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
bool createdNew = true;
using (Mutex mutex = new Mutex(true, "myappliaction", out createdNew))
{

try
{
if (createdNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new mainform());
}
else
{
MessageBox.Show("Application is already running.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
//Process current = Process.GetCurrentProcess();
//foreach (Process process in Process.GetProcessesByName(current.ProcessName))
//{
// if (process.Id != current.Id)
// {
// SetForegroundWindow(process.MainWindowHandle);
// break;
// }
//}
}
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
}
}
}
}

No comments:

Post a Comment