InProgramI have been using this method to control the running of a single instance:
Bool createnew = true;
Using (mutex M = new mutex (false, "X8127891C-CD52-4448-B0FF-AB5E5AA34F15", out createnew ))
{
If (createnew = false)
{
MessageBox. Show ("this program is running and cannot run multiple instances at the same time", "prompt", messageboxbuttons. OK, messageboxicon. Error );
Return;
}
.....
The program has been running well, but a problem was suddenly found some time ago: when using the Remote Desktop login of the operating system, multiple program instances can be created, that is, different sessions can successfully run the program, only one instance can be created in the same session. I checked msdn and said that mutex is at the operating system level. Why?
There is no time to explore, but I just made a simple change (although the program with the same name is different, it cannot run at the same time. Fortunately, this program is not a general product and it is unlikely to have the same name ):
Process [] PS = process. getprocessesbyname (process. getcurrentprocess (). processname );
If (PS. length> = 2)
{
MessageBox. Show ("this program is running and cannot run multiple instances at the same time", "prompt", messageboxbuttons. OK, messageboxicon. Error );
Return;
}
.....