C # determine whether the current startup process exists

Source: Internet
Author: User

System. Threading. Mutex mutex; // note that this variable must be defined as a member variable rather than a local variable.
Bool CheckMultiInstance ()
{
Bool createdNew = true;
Mutex = new System. Threading. Mutex (true, "_ TEST_Mutex _", out createdNew); // each user can start a program process under multiple users in the System.
Mutex = new System. threading. mutex (true, "Global \ _ TEST_Mutex _", out createdNew); // in the system, no matter how many users, only one such program process exists.

If (! CreatedNew)
{
MessageBox. Show ("Program is already running .");
}
Return createdNew;
}

/// <Summary>
/// Is exist same process
/// </Summary>
/// <Returns> </returns>
Public static Process GetExistProcess ()
{
Try
{
Var current = Process. GetCurrentProcess ();
Var pArrayy = System. Diagnostics. Process. GetProcessesByName (current. ProcessName );
If (pArrayy = null | pArrayy. Length = 0)
{
Return null;
}
Foreach (var item in pArrayy)
{
Try
{
If (! Item. ProcessName. ToUpper (). Contains (current. ProcessNameToUpper ()))
{
Continue;
}
If (item. Id! = Current. Id & item. MainModule. ModuleName. ToUpper () = current. MainModule. ModuleName. ToUpper ())
{
Return item;
}
}
Catch (Exception er)
{
LogHelper. WriteException (er );
Continue;
}

}
}
Catch (Exception ex)
{
LogHelper. WriteException (ex );
}
Return null;
}
 


Author: haylhf

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.