如何使用命名的Mutex

时间:2017-04-26 14:20:52

标签: c# .net multithreading mutex streamwriter

我试图使用Mutex处理不同的进程/应用程序以在同一文件上写入。

这是我的代码

 ILogTest logTest = new LogTest(new FileLog());
 logTest.PerformanceTest();


    public class ILogTest
    {
            public void PerformanceTest()
            {
                for (int i = 0; i < this.numberOfIterations; i++)
                {
                    try
                    {
                        Thread threadC = Thread.CurrentThread;
                        threadC = new Thread(ThreadProc);
                        threadC.Name = i.ToString();
                        threadC.Start();
                        threadC.Suspend();
                        threadC.IsBackground = true;
                    }
                    catch (Exception)
                    {
                        throw new Exception("errore");
                    }
                }
            }

            private void ThreadProc()
            {
                try
                {
                    log.Write("Thread : " + Thread.CurrentThread.Name.ToString());
                    this.log.Write("Thread : " + Thread.CurrentThread.Name.ToString());
                    this.log.Write("Thread : " + Thread.CurrentThread.Name.ToString());
                    this.log.Write("Thread : " + Thread.CurrentThread.Name.ToString());
                }
                catch (Exception)
                {
                    throw new Exception("errore");
                }
            }
    }

FileLog是ILogTest的一个实现。

写方法:

public void Write(string message)
    {
        try
        {
            rwl.WaitOne();
            try
            {
                string tID = Thread.CurrentThread.ManagedThreadId.ToString(CultureInfo.CurrentCulture);

                sw.WriteLine(sev.ToString() + "\t" + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss", CultureInfo.CurrentCulture) + "\t\t"  + System.Reflection.Assembly.GetCallingAssembly().GetName().Name + "\t" + Process.GetCurrentProcess().Id.ToString(CultureInfo.CurrentCulture) + "\t" + tID + " \t " + message);


                sw.WriteLine("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
            }
            catch (Exception ex)
            {
                throw new ArgumentException("Cannot write to file " + ex.Message);
            }
            finally
            {
                rwl.ReleaseMutex();
            }
        }
        catch (ApplicationException)
        {
        }
    }

FileLog main:

public FileLog()
    {
        try
        {
            rwl.WaitOne();
            string filePath = Path.GetTempPath() + "Test.txt";
            if (!File.Exists(filePath))
            {
                swa = new FileStream(filePath, FileMode.Append, FileAccess.Write);
                sw = new StreamWriter(swa);
            }
        }
        catch (Exception ex)
        {
            throw new ArgumentException("Cannot open or create file " + ex.Message);
        }

        try
        {
            if (sw == null)
            {
                swa = new FileStream(filePath, FileMode.Append, FileAccess.Write);
                sw = new StreamWriter(swa);
            }

            sw.AutoFlush = true;
        }
        catch (Exception ex)
        {
            throw new ArgumentException("Cannot write to file " + ex.Message);
        }
    }

尝试模拟它,它不会写任何文件,只创建它.. 我不知道为什么。 谁能帮助我?感谢

1 个答案:

答案 0 :(得分:1)

FileLog构造函数调用rwl.WaitOne()获取互斥锁(我假设rwlMutex个对象),但从不调用rwl.ReleaseMutex()。< / p>

因此,调用Write()的每个线程在调用rwl.WaitOne()时都会阻塞,因为互斥锁仍由另一个线程拥有。

您需要在Mutex构造函数中释放FileLog,以便其他线程可以获取它。

此外,您在threadC.Suspend();打电话给PerformanceTest;除非有代码恢复线程,否则它们永远不会完成运行。