Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Windows. forms;
Using system. diagnostics;
Using system. runtime. interopservices;
Using system. reflection;
Namespace onLyprocess
{
Static class Program
{
[Stathread]
Static void main ()
{
MessageBox. Show ("ddddd ");
Process instance = runninginstance ();
If (instance = NULL)
{
System. Windows. Forms. application. enablevisualstyles (); // these two rows allow the form to have the XP style.
System. Windows. Forms. application. doevents ();
Application. Run (New form1 ());
}
Else
{
Handlerunninginstance (instance );
}
}
# Region only runs one instance
Public static process runninginstance ()
{
Process current = process. getcurrentprocess (); // the current new thread
Process [] processes = process. getprocessesbyname (current. processname );
// Traverse the process list with the same name as the current process
Foreach (Process in processes)
{
// Process. The original thread ID is different from the current new thread ID.
// Ignore the current process
If (process. ID! = Current. ID)
{
// Make sure that the process is running from the EXE file.
If (assembly. getexecutingassembly (). Location. Replace ("/", "\") = current. mainmodule. filename)
{
// Return the other process instance.
Return process; // return the form of the original thread
}
}
}
Return NULL;
}
Private Static void handlerunninginstance (process instance)
{
MessageBox. Show ("this application system is already running! "," Message ", messageboxbuttons. OK, messageboxicon. information );
Showwindowasync (instance. main1_whandle, ws_shownormal); // call the API function and display the window normally.
Setforegroundwindow (instance. main1_whandle); // place the window at the front end.
}
[DllimpORT ("user32.dll")]
Private Static extern bool showwindowasync (system. intptr hwnd, int cmdshow );
[DllimpORT ("user32.dll")]
Private Static extern bool setforegroundwindow (system. intptr hwnd );
Private const int ws_shownormal = 1;
# Endregion
}
}