Read / write C # multithreading text

After a lot of research, and after reading and considering all the questions here, I think it's time for me to ask for help.

I have an application in C # and I'm trying to write a file with a different stream to SAME.

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

I tried everything, my code around the world is similar, but I try my best, with every lock, every way to open a file, but I keep getting The process cannot access the files because it in use . The line that generates this error is this using (TextWriter myWriter = new StreamWriter(path_file)) .

I tried a lot, closing files, etc., but when the threads start working at the same time, the program stops and gives me an error The process cannot access the files because it in use (self-explain). But I don’t understand why blocking involves blocking another thread to enter here. And I used the Synchronized method for recording, which is thread safe. Well, sorry for writing this first post here.

+5
source share
3 answers

The synchronized writer must still be deleted:

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

In addition, you can save the sync variable and call its methods from all threads, and it will do all the work for you, and you can delete it later after all the text has been written.

0
source

I am testing your code and applying some changes, and it works fine without errors.

I used the winform application to run your code. Please check the code below.

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

I got the output as shown below.

  • 0 - test Thread: ThreadsWriters1
  • 0 - test Thread: ThreadsWriters2
  • 1 - test Thread: ThreadsWriters2
  • 2 - test Thread: ThreadsWriters2
  • 1 - test Thread: ThreadsWriters1
  • ....
  • ....
0
source

It seems you want to write lines to a single file from multiple streams.

It is very inefficient to read all the lines every time you want to print a line, then add a line and write it out.

I think the reason for your error is that you are not disposing of Synchronized TextWriter, so it is possible that the link to the file remains open, even if you are disposing of the underlying TextWriter.

It is also redundant to use your own lock and a synchronized text editor.

I modified your code a bit to split a single synchronized TextWriter and just add lines, rather than read the whole file, add, and then write it:

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

Source: https://habr.com/ru/post/1263744/


All Articles