使用系统范围的Mutex

时间:2019-03-07 08:48:10

标签: c# winforms mutex

创建一个可与同一应用程序的许多实例一起使用的“ System Wide Mutex”时,我遇到一个问题。

所以我希望一个进程具有线程安全性,正如我在写入同一文件的代码中所见:
写入相同的文件

我想知道是否以编程方式正确设置了此设置。例如,当我关闭表单时,我释放互斥量。在创建互斥锁时,我还将布尔值设置为true(InitiallyOwned)吗?

我已经尝试过用Google搜索它,但不确定是否能得到一个明确的答案。 用例将是能够随机打开和关闭实例,并始终保持互斥体。

public Form1()
{
    try
    {
        _mutex = System.Threading.Mutex.OpenExisting("systemWideMutex");
        _mutex.WaitOne(); //obtain a lock by waitone
        _mutex.ReleaseMutex(); //release
    }
    catch { _mutex = new System.Threading.Mutex(true, "systemWideMutex"); } //Create mutex for the first time

    new Thread(threadSafeWorkBetween2Instances).Start(); //Start process
}
void threadSafeWorkBetween2Instances()
{
    while (true)
    {
        try
        {
            _mutex = System.Threading.Mutex.OpenExisting("systemWideMutex");
            _mutex.WaitOne(); //obtain a lock by waitone

            //DO THREADSAFE WORK HERE!!!
            //Write to the same file

            _mutex.ReleaseMutex(); //release
        }
        catch { _mutex = new System.Threading.Mutex(true, "systemWideMutex"); } //Create mutex for the first time
        Thread.Sleep(10000);
    }
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    _mutex.ReleaseMutex(); //release
}

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

     System.Threading.Mutex _mutex = null;
bool mutexWasCreated = false;
                public Form1()
                {
                    new Thread(threadSafeWorkBetween2Instances).Start();
                }
                void threadSafeWorkBetween2Instances()
                {
                     if(!_mutex.TryOpenExisting("GlobalsystemWideMutex"))
                     {
                        // Create a Mutex object that represents the system
                       // mutex named with
                       // initial ownership for this thread, and with the
                       // specified security access. The Boolean value that 
                       // indicates creation of the underlying system object
                       // is placed in mutexWasCreated.
                         //
                        _mutex = new Mutex(true, "GlobalsystemWideMutex", out 
                         mutexWasCreated, mSec);

                        if(!mutexWasCreated )
                        {
                              //report error
                         }
                     } 
                    while (true)
                    {
                        try
                        {                       
                            bool acquired = _mutex.WaitOne(5000); //obtain a lock - timeout after n number of seconds
                            if(acquired)
                            {   
                               try 
                               {
                                  //DO THREADSAFE WORK HERE!!!
                                  //Write to the same file
                               }
                               catch (Exception e)
                               {

                               }  
                               finally 
                               {
                                  _mutex.ReleaseMutex(); //release
                                  break;
                               }                          
                            }
                            else
                            {
                               Thread.Sleep(1000); // wait for n number of seconds before retrying
                            } 
                        }
                        catch {  } //Create mutex for the first time

                    }
                }
                private void Form1_FormClosing(object sender, FormClosingEventArgs e)
                {
                    try { _mutex.ReleaseMutex(); } catch { } //release
                }