I am currently working on a job scheduling leleapp. This article does not discuss the Job Scheduling section (which will be written later). This article only discusses how to ensure the running of a single instance.
Eagleapp running requirements
1. Start the application when the system starts;
2. The instance is running in each session;
When you enable a Remote Desktop Connection, The leleapp allows only one instance to be started.
3. Only one instance can run in the system;
When multiple Remote Desktop links are enabled, the leleapp allows only one instance to be started.
Mutex
Mutex (mutex ):A synchronization primitive can also be used for inter-process synchronization.
Visible msnd: http://msdn.microsoft.com/zh-cn/library/system.threading.mutex.aspx
In the introduction of the official document, there is such a paragraph
On the server that runs the terminal service, the named system mutex can have two levels of visibility. If the name is prefixed with "Global \", mutex is visible in all Terminal Server sessions. If the name is prefixed with "Local \", mutex is only visible in the terminal server session where it is created. In this case, each other Terminal Server session on the server can have an independent mutex with the same name. If you do not specify a prefix when creating a named mutex, it uses the prefix "Local \". In a Terminal Server session, only two mutex with different name prefixes are independent mutex, which are visible to all processes in the terminal server session. That is, the prefix names "Global \" and "Local \" indicate the scope of the mutex name relative to the terminal server session rather than the process.
Unfortunately, the C # example is not found. This section can be understood:
Mutexname ="Local\ "+" Appname ";
This parameter is only valid for sessions that are created. In other words, when a remote connection is enabled, the eagleapp instance is started again.
Mutexname = "Global \" + "appname ";
Valid for all sessions. When multiple remote connections are enabled, only the first remote connection starts the consoleapp instance.
Run an instance in each session Status
Private Static Void Localmutex ()
{
// Whether to create mutex for the first time
Bool Newmutexcreated = False ;
String Mutexname = " Local \\ " + " Tenghoo " ;
Mutex = Null ;
Try
{
Mutex = New Mutex ( False , Mutexname, Out Newmutexcreated );
}
Catch (Exception ex)
{
Console. Write (ex. Message );
System. Threading. thread. Sleep ( 3000 );
Environment. Exit ( 1 );
}
// Create mutex for the first time
If (Newmutexcreated)
{
Console. writeline ( " ProgramStarted " );
// Todo: the task to be executed.
}
Else
{
Console. Write ( " The other window is already running and will be automatically closed 3 seconds later .. " );
System. Threading. thread. Sleep ( 1000 );
Console. Write ( " 1 " );
System. Threading. thread. Sleep ( 1000 );
Console. Write ( " 2 " );
System. Threading. thread. Sleep ( 1000 );
Console. Write ( " 3 " );
Environment. Exit ( 1 ); // Exit Program
}
}
Run a single instance within the system
Private Static Void Globalmutex ()
{
// Whether to create mutex for the first time
Bool Newmutexcreated = False ;
String Mutexname = " Global \\ " + " Tenghoo " ;
Mutex = Null ;
Try
{
Mutex = New Mutex ( False , Mutexname, Out Newmutexcreated );
}
Catch (Exception ex)
{
Console. Write (ex. Message );
System. Threading. thread. Sleep ( 3000 );
Environment. Exit ( 1 );
}
// Create mutex for the first time
If (Newmutexcreated)
{
Console. writeline ( " Program started " );
// Todo: the task to be executed.
}
Else
{
Console. Write ( " The other window is already running and will be automatically closed 3 seconds later .. " );
System. Threading. thread. Sleep ( 1000 );
Console. Write ( " 1 " );
System. Threading. thread. Sleep ( 1000 );
Console. Write ( " 2 " );
System. Threading. thread. Sleep ( 1000 );
Console. Write ( " 3 " );
Environment. Exit ( 1 ); // Exit Program
}
}