【问题标题】:Multithreading lock read/write text c#多线程锁读/写文本c#
【发布时间】:2017-06-19 22:20:03
【问题描述】:

经过大量研究,阅读并尝试了这里的所有问题后,我认为是时候寻求帮助了。

我有一个 C# 应用程序,我正在尝试使用不同的线程写入相同的文件。

public static void LaunchThreads(string path_file)
{
    int i = 0;
    Dictionary<int, Thread> threadsdico = new Dictionary<int, Thread>();
    while (i < MAX_THREAD)
    {
            Thread thread = new Thread(() => ThreadEntryWriter(string path_file));
            thread.Name = string.Format("ThreadsWriters{0}", i);
            threadsdico.Add(i, thread);
            thread.Start();
            i++;
    }
    int zz = 0;
    while (zz < threadsdico.Count())
    {
        threadsdico[zz].Join();
        zz++;
    }
}
private static readonly Object obj = new Object();
public static void ThreadEntryWriter(string path_file)
{
    int w = 0;
    while (w < 99)
    {       
        string newline = w + " - test" + "\r";
        lock(obj)
        {
            string txt = File.ReadAllText(path_file);
            using (TextWriter myWriter = new StreamWriter(path_file))
            {   
                TextWriter.Synchronized(myWriter).Write(txt + newline);
            }
        }
        w++;
    }
}

我已经尝试了所有方法,我的代码在全局范围内都是这样的,但是我尝试了所有方法,每个锁,每个文件打开方法,但我不断收到The process cannot access the files because it's in use。产生此错误的行是using (TextWriter myWriter = new StreamWriter(path_file))

我尝试了很多东西,关闭文件等,但是当线程同时开始工作时,程序停止并给我错误The process cannot access the files because it's in use(自我解释)。但我不明白为什么,锁是为了阻止另一个线程进入这里。我使用同步方法来编写线程安全的。抱歉写了这么长,这是我在这里的第一篇文章。

【问题讨论】:

  • "程序停止并给我错误。" - 你能分享你的错误吗?
  • 您确定没有其他代码可以打开该文件吗?如果是,请关闭防病毒扫描程序并重试。
  • 错误类似于“进程无法访问文件,因为它正在使用中”。
  • TextWriter.Synchronized 返回一个新实例(原始 TextWriter 的包装器)。也许你也需要Dispose那个实例?
  • 是本地文件还是网络共享文件?顺便说一句,我试图重现这个问题,但我的程序在 MAX_THREAD=10 时运行良好。 (只需添加主函数包装器)

标签: c# multithreading c#-4.0 thread-safety locking


【解决方案1】:

同步写入器仍需处理:

var newline = w + " - test";
using (var sw = new StreamWriter(path_file))
using (var sync = TextWriter.Synchronized(sw))
{
    // no need to add a new line char, just use other method to write
    sync.WriteLine(txt + newline);
}

另外,您可以保存一个sync 变量并从所有线程调用它的方法,它会为您完成所有工作,您可以在编写完所有文本后将其释放。

【讨论】:

    【解决方案2】:

    我测试了您的代码并应用了一些更改,它运行良好且没有错误。

    我使用 winform 应用程序运行您的代码。请检查以下代码。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication14
    {
        public partial class Form1 : Form
        {
            private readonly Object obj = new Object();
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                LaunchThreads("D:\\log.txt");
            }
    
            public void LaunchThreads(string path_file)
            {
                int i = 0;
                int MAX_THREAD = 10;
                Dictionary<int, Thread> threadsdico = new Dictionary<int, Thread>();
                while (i < MAX_THREAD)
                {
                    Thread thread = new Thread(() => ThreadEntryWriter(path_file, string.Format("ThreadsWriters{0}", i)));
                        thread.Name = string.Format("ThreadsWriters{0}", i);
                        threadsdico.Add(i, thread);
                        thread.Start();
                        i++;
                }
                int zz = 0;
                while (zz < threadsdico.Count())
                {
                    threadsdico[zz].Join();
                    zz++;
                }
            }
    
            public void ThreadEntryWriter(string path_file,string threadName)
            {
                int w = 0;
                while (w < 99)
                {
                    string newline = w + " - test" + " Thread:" + threadName + "\r\n";
                    lock (obj)
                    {
                        string txt = File.ReadAllText(path_file);
                        using (TextWriter myWriter = new StreamWriter(path_file))
                        {
                            TextWriter.Synchronized(myWriter).Write(txt + newline);
                        }
                    }
                    w++;
                }
            }
        }
    }
    

    我得到如下输出。

    • 0 - 测试线程:ThreadsWriters1
    • 0 - 测试线程:ThreadsWriters2
    • 1 - 测试线程:ThreadsWriters2
    • 2 - 测试线程:ThreadsWriters2
    • 1 - 测试线程:ThreadsWriters1
    • ....
    • ....

    【讨论】:

      【解决方案3】:

      您似乎想从多个线程向同一个文件写入行。

      每次你想输出一行然后追加一行然后写出来的时候,读取所有的行是非常低效的。

      我认为您的错误的原因是您没有处置 Synchronized TextWriter,因此即使您处置了底层 TextWriter,它也可能保持对文件的引用处于打开状态。

      使用您自己的锁定和 Synchronized TextWriter 也是多余的。

      我对您的代码进行了一些更改,以共享一个 Synchronized TextWriter 并仅追加行,而不是读取整个文件,追加然后将其写出:

          public static void LaunchThreads(string path_file)
          {
              int i = 0;
              using(var sw = File.CreateText(path_file))
              using(var syncWriter = TextWriter.Synchronized(sw)) 
              {
                  Dictionary<int, Thread> threadsdico = new Dictionary<int, Thread>();
                  while (i < MAX_THREAD)
                  {
                      Thread thread = new Thread(() => ThreadEntryWriter(syncWriter));
                      thread.Name = string.Format("ThreadsWriters{0}", i);
                      threadsdico.Add(i, thread);
                      thread.Start();
                      i++;
                  }
                  int zz = 0;
                  while (zz < threadsdico.Count())
                  {
                      threadsdico[zz].Join();
                      zz++;
                  }
              }
          }
      
          public static void ThreadEntryWriter(TextWriter writer)
          {
              int w = 0;
              while (w < 99)
              {       
                  writer.WriteLine($"{w} - test");
                  w++;
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2015-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多