In Main () code to call a function I have many classes that should work through Thread, for example:

//да что показывать всё просто в programs вызываю в Main все необходимые классы!) using System; using System.Threading; namespace Test { internal class Program { static void Main(string[] args) { Thread s = new Thread(class.Go); s.Start(); Thread s = new Thread(class2.Go); s.Start(); Thread s = new Thread(class3.Go); s.Start(); Thread s = new Thread(class4.Go); s.Start(); Thread s = new Thread(class5.Go); s.Start(); } } } 

To not write so much for each class, how can this be done better?

  • Is this code in one function, or is it different in each class? If in one function, do a loop. - VladD
  • I call it all in grade 1 .. - GooliveR
  • Then it is not clear why not a cycle. Show more code. - VladD
  • Yes, what to show)) .. Everything is simple in the program call .. So I think it would be better not to write for everyone - GooliveR
  • one
    OK. Prompt, still these yours class..class5 these are exactly class names, and Go is a static method? Or are these objects of different classes, and Go methods of instances? - 4per

2 answers 2

So it can be even shorter:

 var actions = new ThreadStart[] { class.Go, class2.Go, class3.Go, class4.Go, class5.Go }; foreach (var action in actions) new Thread(action).Start(); 

For option with Task :

 var actions = new Action[] { class.Go, class2.Go, class3.Go, class4.Go, class5.Go }; foreach (var action in actions) Task.Run(action); 

But there is a subtlety: the tasks are performed in the background thread - and, most likely, you will want to wait for them to complete before finishing the work. In this case, you can do this:

 var actions = new Action[] { class.Go, class2.Go, class3.Go, class4.Go, class5.Go }; var tasks = actions.Select(action => Task.Run(action)).ToArray(); // ... Task.WaitAll(tasks); 

Or like this:

 var actions = new Action[] { class.Go, class2.Go, class3.Go, class4.Go, class5.Go }; var task = Task.WhenAll(actions.Select(action => Task.Run(action))); // ... task.Wait(); 
  • And if I need to wait for the execution by adding Join (); for a particular class, and then continue ... Where in example 1 can this be done? - GooliveR
  • @ArteS you said that your code is the same ... Where did a certain class come from? - Pavel Mayorov

Solution 1 and 2 is applicable for static methods, 3 is not.

one

 Thread[] threads = new Thread[] { new Thread(class1.Go), new Thread(class2.Go), new Thread(class3.Go), new Thread(class4.Go), new Thread(class5.Go) }; foreach(var thr in threads) thr.Start(); 

2

 var thrstarts = new ThreadStart[] { new ThreadStart(class0.Go), new ThreadStart(class1.Go), new ThreadStart(class2.Go), new ThreadStart(class3.Go), new ThreadStart(class4.Go), new ThreadStart(class5.Go) }; foreach (var thrstart in thrstarts) { new Thread(thrstart).Start(); } 

3

If we have several classes that have a Go () method with the same signature.

Create interface

 public interface ICanGo { void Go(); } 

Note that classes implement the interface.

 class Class5 : ICanGo { ... 

Call

 ICanGo[] arr = new ICanGo[] { new Class1(), new Class2(), new Class3(), new Class4(), new Class5() }; foreach (var item in arr) { new Thread(item.Go).Start(); } 

PS how it was taught on ruSo, Thread in this context is only used in .Net 3.5 and below. Above there is a Task

PPS interface creation is still useful even with Task

  • Already something) Thank you) And for Task like? - GooliveR
  • @ArteS And what about something like that? Task , unlike Thread , is created and started with one method call - Pavel Mayorov