C # only allows two methods to start a winfrom process

Source: Internet
Author: User
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)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.