There are 6 most normal streams, but I need to save each stream in txt. When I write, then an error pops up that the file is already in use.

How to be, if you still need to save?


using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Threading; using System.Threading.Tasks; namespace Threads { class Program { private static void Save(string data) { StreamWriter writer = new StreamWriter("C:/Users/Wiz/Desktop/test.txt", true, Encoding.GetEncoding("windows-1251")); writer.Write(data + Environment.NewLine); writer.Close(); } static string str = "abcdefghijklmnopqrstuvwxyz"; static void Main(string[] args) { Thread th1 = new Thread(thread1); th1.Start(); Thread th2 = new Thread(thread2); th2.Start(); Thread th3 = new Thread(thread3); th3.Start(); Thread th4 = new Thread(thread4); th4.Start(); Thread th5 = new Thread(thread5); th5.Start(); Thread th6 = new Thread(thread6); th6.Start(); } static void thread1() { for (int a = 0; a < str.Length; a++) { for (int b = 0; b < str.Length; b++) { for (int c = 0; c < str.Length; c++) { for (int d = 0; d < str.Length; d++) { string s111 = str[a].ToString() + str[b].ToString() + str[c].ToString() + str[d].ToString(); Console.WriteLine(s111); Save(s111); } } } } } static void thread2() { for (int a = 0; a < str.Length; a++) { for (int b = 0; b < str.Length; b++) { for (int c = 0; c < str.Length; c++) { for (int d = 0; d < str.Length; d++) { string s111 = str[a].ToString() + str[b].ToString() + str[c].ToString() + str[d].ToString(); Console.WriteLine(s111); Save(s111); } } } } } static void thread3() { for (int a = 0; a < str.Length; a++) { for (int b = 0; b < str.Length; b++) { for (int c = 0; c < str.Length; c++) { for (int d = 0; d < str.Length; d++) { string s111 = str[a].ToString() + str[b].ToString() + str[c].ToString() + str[d].ToString(); Console.WriteLine(s111); Save(s111); } } } } } static void thread4() { for (int a = 0; a < str.Length; a++) { for (int b = 0; b < str.Length; b++) { for (int c = 0; c < str.Length; c++) { for (int d = 0; d < str.Length; d++) { string s111 = str[a].ToString() + str[b].ToString() + str[c].ToString() + str[d].ToString(); Console.WriteLine(s111); Save(s111); } } } } } static void thread5() { for (int a = 0; a < str.Length; a++) { for (int b = 0; b < str.Length; b++) { for (int c = 0; c < str.Length; c++) { for (int d = 0; d < str.Length; d++) { string s111 = str[a].ToString() + str[b].ToString() + str[c].ToString() + str[d].ToString(); Console.WriteLine(s111); Save(s111); } } } } } static void thread6() { for (int a = 0; a < str.Length; a++) { for (int b = 0; b < str.Length; b++) { for (int c = 0; c < str.Length; c++) { for (int d = 0; d < str.Length; d++) { string s111 = str[a].ToString() + str[b].ToString() + str[c].ToString() + str[d].ToString(); Console.WriteLine(s111); Save(s111); } } } } } } } 

Closed due to the fact that the question is too general for the participants Dmitriy Simushev , cheops , Alexey Shimansky , Pavel Mayorov , Nick Volynkin ♦ 17 May '16 at 6:56 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Comments are not intended for extended discussion; conversation moved to chat . - Nicolas Chabanovsky ♦

1 answer 1

Wrap in a lock procedure for writing to a file.

 private static object _locker = new object(); private static void Save(string data) { lock (_locker) { StreamWriter writer = new StreamWriter("C:/Users/Wiz/Desktop/test.txt", true, Encoding.GetEncoding("windows-1251")); writer.Write(data + Environment.NewLine); writer.Close(); } }