多线程锁读写文本 C#

5

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

我有一个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++;
    }
}

我已经尝试了各种方法,我的代码是全局的,但是我已经尝试了各种方法,包括每种锁、每种文件打开方法,但我仍然不断收到“因为正在使用而无法访问文件”的错误消息。生成此错误的行是:using (TextWriter myWriter = new StreamWriter(path_file))
我尝试了很多方法,比如关闭文件等,但当多个线程同时开始工作时,程序会停止并给出错误消息因为正在使用而无法访问文件(自解释)。但我不明白为什么,锁应该阻止另一个线程进入此处。而我使用了Synchronized方法进行编写,这是线程安全的。对于长篇大论,敬请谅解,这是我在这里的第一篇帖子。

2
你确定没有其他打开该文件的代码吗?如果是,请关闭防病毒扫描器并重试。 - Alexei Levenkov
如果您使用 File.WriteAllText 而不是流写入器,会出现相同的错误吗? - Ivan Leonenko
4
TextWriter.Synchronized 返回一个新实例(原始 TextWriter 的包装器)。也许您也需要 "Dispose" 该实例? - John Wu
2
它是本地文件还是网络共享文件?顺便说一下,我尝试重现了这个问题,但我的程序在MAX_THREAD=10时可以正常运行。(只是添加了main函数包装器) - jiulongw
似乎故障发生在原始代码中,而不是发布的代码中。没有看到故障部分的代码,如何回答这个问题呢?水晶球可不常见... - Sir Rufo
显示剩余13条评论
3个回答

0

我测试了你的代码并进行了一些更改,现在它可以正常运行,没有出现错误。

我使用了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
  • ....
  • ....

0

同步写入器仍应该被释放:

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变量,并从所有线程调用其方法,它将为您完成所有工作,然后在所有文本编写完成后将其处理掉。

0

看起来你想从多个线程向同一个文件写入行。

每次想要输出一行时,读取所有行然后附加一行并将其写出是相当低效的。

我认为你的错误原因是你没有处理同步的TextWriter,所以即使你处理了底层的TextWriter,它可能仍然保持对文件的引用。

同时使用自己的锁和同步的TextWriter也是多余的。

我已经稍微修改了你的代码,共享一个同步的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++;
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接