SoFunction
Updated on 2025-03-08

Use mutex to implement single instance of application running code sharing


class Test
     {
         /// <summary>
/// The main entry point of the application.
         /// </summary>
          [STAThread]
         static void Main(string[] args)
         {
              bool flag=false;
              mutex=new (true,"Test",out flag);
//First parameter: true--Give the initial ownership of the mutex to the calling thread
//First parameter: name of the mutex
//The third parameter: return value, if the calling thread has been granted the initial ownership of the mutex, return true
              if(flag)
              {
                   ("Running");
              }
              else
              {
                   ("Another is Running");
(5000);//The thread suspends for 5 seconds
(1);//Exit the program
              }
              ();
         }