C # Only One winform process can be run

Source: Internet
Author: User

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

}

}

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.