How to finish the program correctly:

class Program { static void Main(string[] args) { List<double> mass = new List<double>() { 1, 2, 3, 6, 7, 13, 21 }; Analysis analysis = new Analysis(mass); if (analysis.task1.IsAlive == false && analysis.task2.IsAlive == false) { analysis.Show_Res(); } Console.ReadKey(); } } public class Analysis { List<double> mass; public Thread task1; public Thread task2; public Analysis(List<double> mass) { this.mass = mass; task1 = new Thread(Fibonachi); task2 = new Thread(Prime_Numbers); task1.Start(); task2.Start(); } public void Show_Res() { StreamReader read1 = new StreamReader("Fibonachi.txt"); string line = ""; Console.WriteLine("Найденны числа фибоначи"); while ((line = read1.ReadLine()) != null) { Console.WriteLine(line); } read1.Close(); StreamReader read2 = new StreamReader("Prime_Numbers.txt"); Console.WriteLine("Найдены простые числа"); while ((line = read2.ReadLine()) != null) { Console.WriteLine(line); } read2.Close(); } public void Fibonachi() { StreamWriter str1 = new StreamWriter("Fibonachi.txt"); for (var i = 2; i < mass.Count; i++) { if (mass[i - 2] + mass[i - 1] == mass[i]) { str1.WriteLine(mass[i]); } } str1.Close(); task1.Abort(); } public void Prime_Numbers() { StreamWriter str = new StreamWriter("Prime_Numbers.txt"); bool prost; for (var i = 0; i < mass.Count; i++) { prost = true; for (int j = 2; j <= mass[i] / 2; j++) { if (mass[i] % j == 0) { prost = false; break; } } if (prost) { str.WriteLine(mass[i]); } } str.Close(); task2.Abort(); } } 

How can I call this 3 Show_Res method, I haven’t tried it, I don’t understand how to synchronize this damned chariot with me, for me multithreading is the most painful topic for me, please tell me. If with explanations I will be at many grateful! (

  • Well, what are you doing then at least describe? And what exactly is the problem? And then, knowing the task, you cannot solve the problems, but we offer you to guess everything. - tym32167 pm
  • @ tym32167, two streams, one looking for Fibonacci numbers, the second simple, write the numbers found in files. Upon completion of the work flow to read the files and enter the result. I can not withdraw my output works before they are completed - Valera
  • Do you need to solve the task with threads? Or can it be done? - tym32167
  • @ tym32167, create 2 streams according to the task. And yet, there are no clarifications - Valera
  • one
    Well, just two, then in my answer the third thread can not be created, but the function can be called directly - tym32167 5:43 pm

1 answer 1

To wait for the end of the thread, you must call myThread.Join(); , thread.Abort(); no reason to call. Added a minimum of changes to your code, but did not check, did not run. Ideally, in general, use tasks instead of streams.

 class Program { static void Main(string[] args) { List<double> mass = new List<double>() { 1, 2, 3, 6, 7, 13, 21 }; Analysis analysis = new Analysis(mass); analysis.task1.Join(); analysis.task2.Join(); var task3 = new Thread(analysis.Show_Res); task3.Start(); task3.Join(); Console.ReadKey(); } } public class Analysis { List<double> mass; public Thread task1; public Thread task2; public Analysis(List<double> mass) { this.mass = mass; task1 = new Thread(Fibonachi); task2 = new Thread(Prime_Numbers); task1.Start(); task2.Start(); } public void Show_Res() { using (StreamReader read1 = new StreamReader("Fibonachi.txt")) { string line = ""; Console.WriteLine("Найденны числа фибоначи"); while ((line = read1.ReadLine()) != null) { Console.WriteLine(line); } } using (StreamReader read2 = new StreamReader("Prime_Numbers.txt")) { string line = ""; Console.WriteLine("Найдены простые числа"); while ((line = read2.ReadLine()) != null) { Console.WriteLine(line); } } } public void Fibonachi() { using (StreamWriter str1 = new StreamWriter("Fibonachi.txt")) { for (var i = 2; i < mass.Count; i++) { if (mass[i - 2] + mass[i - 1] == mass[i]) { str1.WriteLine(mass[i]); } } } } public void Prime_Numbers() { using (StreamWriter str = new StreamWriter("Prime_Numbers.txt")) { bool prost; for (var i = 0; i < mass.Count; i++) { prost = true; for (int j = 2; j <= mass[i] / 2; j++) { if (mass[i] % j == 0) { prost = false; break; } } if (prost) { str.WriteLine(mass[i]); } } } } }