C # Start only one process

Source: Internet
Author: User

It is necessary to ensure that the software runs only once, and the method is also very simple. You only need to judge whether the current process name exists in the process. For WPF software, it is determined in App. XAML. cs. You can override the OnStartup method. [Csharp] public partial class App: application {// <summary> /// open only one process /// </summary> /// <param name = "e"> </param> protected override void onStartup (StartupEventArgs e) {Process currentProcess = Process. getCurrentProcess (); foreach (Process item in Process. getProcessesByName (currentProcess. processName) {if (item. id! = CurrentProcess. id & (item. startTime-currentProcess. startTime ). totalMilliseconds <= 0) {item. kill (); item. waitForExit (); break;} base. onStartup (e) ;}} public partial class App: application {// <summary> /// open only one process /// </summary> /// <param name = "e"> </param> protected override void onStartup (StartupEventArgs e) {Process currentProcess = Process. getCurrentProcess (); foreach (Process ite M in Process. GetProcessesByName (currentProcess. ProcessName) {if (item. Id! = CurrentProcess. id & (item. startTime-currentProcess. startTime ). totalMilliseconds <= 0) {item. kill (); item. waitForExit (); break;} base. onStartup (e) ;}} for Winform, it is determined directly in the construction of the main form. [Csharp] public partial class Form1: Form {public Form1 () {InitializeComponent (); StartUp ();} private void StartUp () {Process currentProcess = Process. getCurrentProcess (); foreach (Process item in Process. getProcessesByName (currentProcess. processName) {if (item. id! = CurrentProcess. id & (item. startTime-currentProcess. startTime ). totalMilliseconds <= 0) {item. kill (); item. waitForExit (); break ;}}} public partial class Form1: Form {public Form1 () {InitializeComponent (); StartUp ();} private void StartUp () {Process currentProcess = Process. getCurrentProcess (); foreach (Process item in Process. getProcessesByName (currentProcess. processName) {if (item. Id! = CurrentProcess. id & (item. startTime-currentProcess. startTime ). totalMilliseconds <= 0) {item. kill (); item. waitForExit (); break ;}}} simple code to complete this function. Append part: If you want to do more strictly, you can determine the path of the process name: [csharp] www.2cto. comProcess [] sameProcess = Process. getProcessesByName (Path. getFileNameWithoutExtension (AppDomain. currentDomain. baseDirectory + currentProcess. processName); if (sameProcess! = Null & sameProcess. Length> 0) {// here it indicates a duplicate process}

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.