Method One: Only multiple processes are allowed to run
View Plaincopy to Clipboardprint?
········· 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150
[STAThread]
public static void Main ()
{
BOOL ret;
System.Threading.Mutex Mutex = new System.Threading.Mutex (True, application. ProductName, out ret);
if (ret)
{
System.Windows.Forms.Application.EnableVisualStyles (); These two lines implement XP visual style
System.Windows.Forms.Application.DoEvents ();
System.Windows.Forms.Application.Run (New Main ());
Main form for your program, if the console program does not use this sentence
Mutex. ReleaseMutex ();
}
Else
{
MessageBox.Show (NULL, "There is an application that is the same as this program is already running, please do not run multiple programs at the same time. \ n \ nthe program is about to exit. ", Application.productname, MessageBoxButtons.OK, messageboxicon.warning);
The prompt message can be deleted.
Application.exit ();//Exit Program
}
}
[STAThread]
public static void Main ()
{
BOOL ret;
System.Threading.Mutex Mutex = new System.Threading.Mutex (True, Application.productname, out ret);
if (ret)
{
System.Windows.Forms.Application.EnableVisualStyles (); These two lines implement XP visual style
System.Windows.Forms.Application.DoEvents ();
System.Windows.Forms.Application.Run (New Main ());
Main form for your program, if the console program does not use this sentence
Mutex. ReleaseMutex ();
}
Else
{
MessageBox.Show (NULL, "There is an application that is the same as this program is already running, please do not run multiple programs at the same time. \ n \ nthe program is about to exit. ", Application.productname, MessageBoxButtons.OK, messageboxicon.warning);
The prompt message can be deleted.
Application.exit ();//Exit Program
}
}
Method Two: Prevent multiple processes from running and activate the previous process when run repeatedly
View Plaincopy to Clipboardprint?
········· 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150
[STAThread]
public static void Main ()
{
Get the running instance.
Process instance = Runninginstance ();
if (instance = = null)
{
System.Windows.Forms.Application.EnableVisualStyles (); These two lines implement XP visual style
System.Windows.Forms.Application.DoEvents ();
There isn ' t another instance, show our form.
Application.Run (New Main ());
}
Else
{
There is another instance of this process.
Handlerunninginstance (instance);
}
}
public static Process Runninginstance ()
{
Process current = Process.getcurrentprocess ();
Process[] processes = Process.getprocessesbyname (current. ProcessName);
Loop through the running processes in with the same name
foreach (process process in processes)
{
Ignore the current process
if (process. Id! = Current. ID)
{
Make sure, the process is running from the EXE file.
if (assembly.getexecutingassembly (). Location.replace ("/", "\ \") = = Current. Mainmodule.filename)
{
Return the other process instance.
return process;
}
}
}
No Other instance is found, return null.
return null;
}
public static void Handlerunninginstance (Process instance)
{
Make sure the window was not minimized or maximized
Showwindowasync (instance. Mainwindowhandle, Ws_shownormal);
Set the real intance to foreground window
SetForegroundWindow (instance. Mainwindowhandle);
}
[DllImport ("User32.dll")]
private static extern bool Showwindowasync (IntPtr hWnd, int cmdshow);
[DllImport ("User32.dll")]
private static extern bool SetForegroundWindow (IntPtr hWnd);
Private Const int ws_shownormal = 1;
[STAThread]
public static void Main ()
{
Get the running instance.
Process instance = Runninginstance ();
if (instance = = null)
{
System.Windows.Forms.Application.EnableVisualStyles (); These two lines implement XP visual style
System.Windows.Forms.Application.DoEvents ();
There isn ' t another instance, show our form.
Application.Run (New Main ());
}
Else
{
There is another instance of this process.
Handlerunninginstance (instance);
}
}
public static Process Runninginstance ()
{
Process current = Process.getcurrentprocess ();
Process[] processes = Process.getprocessesbyname (current. ProcessName);
Loop through the running processes in with the same name
foreach (process process in processes)
{
Ignore the current process
if (process. Id! = Current. ID)
{
Make sure, the process is running from the EXE file.
if (assembly.getexecutingassembly (). Location.replace ("/", "\ \") = = Current. Mainmodule.filename)
{
Return the other process instance.
return process;
}
}
}
No Other instance is found, return null.
return null;
}
public static void Handlerunninginstance (Process instance)
{
Make sure the window was not minimized or maximized
Showwindowasync (instance. Mainwindowhandle, Ws_shownormal);
Set the real intance to foreground window
SetForegroundWindow (instance. Mainwindowhandle);
}
[DllImport ("User32.dll")]
private static extern bool Showwindowasync (IntPtr hWnd, int cmdshow);
[DllImport ("User32.dll")]
private static extern bool SetForegroundWindow (IntPtr hWnd);
Private Const int ws_shownormal = 1;
The above is the content of the two methods that C # only allows to start a winfrom process, and more about topic.alibabacloud.com (www.php.cn)!