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