Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用命名互斥_C#_.net_Multithreading_Mutex_Streamwriter - Fatal编程技术网

C# 如何使用命名互斥

C# 如何使用命名互斥,c#,.net,multithreading,mutex,streamwriter,C#,.net,Multithreading,Mutex,Streamwriter,我正在尝试使用互斥来处理不同的进程/应用程序,以便在同一个文件上写入 这是我的密码 ILogTest logTest = new LogTest(new FileLog()); logTest.PerformanceTest(); public class ILogTest { public void PerformanceTest() { for (int i = 0; i < thi

我正在尝试使用互斥来处理不同的进程/应用程序,以便在同一个文件上写入

这是我的密码

 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");
                }
            }
    }
主文件日志:

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);
        }
    }
尝试模拟它时,它不向文件写入任何内容,只创建它。。 我不知道为什么。
有人能帮我吗?感谢文件日志
构造函数调用
rwl.WaitOne()
,它获取互斥体(我假设
rwl
是一个
mutex
对象),但从不调用
rwl.ReleaseMutex()

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

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


此外,还可以调用
threadC.Suspend()
性能测试中
;除非有代码恢复线程,否则线程将永远无法完成运行。

文件锁定不是更好吗@AdamBenson,它将要求尝试同时写入的所有其他线程/进程捕获并处理文件当前锁定时引发的
IOException
。根据可能不太理想的争用。考虑使用Log4NET.FILAPPEND,使用锁定模型。如果没有必要,不要重新发明轮子。(你至少可以用它来看看怎么做。)@Christian-hmmm没有意识到C#的实现有那么糟糕。这是一段时间以来,我使用文件锁定在C++中,但我绝对记得能够等待一个文件锁(我记得,因为我死锁一个完整的电视台,并采取了空气与糟糕的实现文件锁……)锁FieleX(Win32 API)将等待,如果你要求:
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);
        }
    }