多线程锁读/写文本 c#



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

我有一个 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给我错误(不言自明)。但我不明白为什么,锁应该阻止另一个线程进入这里。我使用同步方法编写线程安全的方法。很抱歉写得太长了,这是我在这里的第一篇文章。

仍然应该释放同步编写器:

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

我测试了您的代码并应用了一些更改,它工作正常,没有错误。

我使用 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 + "rn";
                lock (obj)
                {
                    string txt = File.ReadAllText(path_file);
                    using (TextWriter myWriter = new StreamWriter(path_file))
                    {
                        TextWriter.Synchronized(myWriter).Write(txt + newline);
                    }
                }
                w++;
            }
        }
    }
}

我得到了如下输出。

  • 0 - 测试线程:线程编写器1
  • 0 - 测试线程:线程编写器2
  • 1 - 测试线程:线程编写器2
  • 2 - 测试线程:线程编写器2
  • 1 - 测试线程:线程编写器1
  • ....
  • ....

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

每次要输出一行然后附加一行并将其写出时,读取所有行的效率非常低。

我认为您错误的原因是您没有处理同步的文本编写器,因此即使您处理了底层文本编写器,它也可能会保持对文件的引用处于打开状态。

使用自己的锁定和同步文本编写器也是多余的。

我已经对您的代码进行了一些更改,以共享单个同步文本编写器,并且仅附加行而不是读取整个文件,附加然后写出:

    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++;
        }
    }

最新更新