Actually a subject. Are there any ways? There is a method, it must be performed many times, the only difference is that for each method there are different input data. I would like to increase the performance of performing these methods in parallel.

  • Without threads, this is not possible. Parallel execution in the system (at least in Win) is implemented through threads. However, you can work with them implicitly - Task and Async in .Net 4.0. - AlexeyM
  • and why it is impossible to use streams? just wondering. moreover, as mentioned above, it is impossible to do this in .Net without the use of threads - all the asynchronous programming patterns are thread-based! - Specter
  • Well, I have several hundred methods to be executed at the same time, there will be performance problems, so I think how to do it - Merlin
  • Determine the number of processor cores in the system, create an appropriate number of threads and assign a queue of running methods to each thread, dividing them all into equal or approximately equal pieces. - skegg
  • one
    ThreadPool automatically performs synchronization and switching between threads, so you can not worry about it, but their number is large enough not to increase, because you can only degrade the performance of PLINQ and Parallel very good job with data independent of each other and work well if you have more than one processor google in more detail ... - Specter

2 answers 2

As far as I know, at the moment only two types of parallelism have been invented: through processes and through threads.

    Use Timer.

     using System; using System.Threading; namespace Timers { class Program { int TimesCalled = 0; void Display (object state) { Console.WriteLine("{0} {1}", (string)state, ++TimesCalled); } static void Main( ) { Program p = new Program(); Timer myTimer = new Timer (p.Display, "Обработка", 2000, // Первый раз метод будет вызван через 2 секунды 1000); // Повтор каждую секунду Console.WriteLine("Таймер запущен."); Console.ReadLine(); } } } 

    When time expires, the timer calls the method in a specific thread, assigning to it the state argument (in our case, the string "Processing").

    In this example, every second, starting from the second, the lines will be displayed:

     "Обработка" 1 "Обработка" 2 "Обработка" 3 "Обработка" 4 "Обработка" 5 ... 

    In order for a timer to call a method, it (the method) must correspond to the TimerCallback delegate:

     void TimerCallback(object state) 
    • one
      Is it with a timer then increase productivity? Nuno - renegator
    • I think the creators of Timer, tried to implement it in such a way that YOU will be able to realize it quickly, then: why reinvent the wheel? - megacoder
    • Speech that Timer calls a method consistently, instead of in parallel, and does it in one flow. - AlexeyM 4:04
    • one
      It's not about implementing your timer. The questioner wants to increase productivity by implementing pseudo-parallel execution of several (hundreds) functions. He does not want to use the regular thread mechanism, because he suspects that it is too expensive. By the time I think so. And your functions are called on a timer once a second. And all this second, the computer is kicking vanya - renegator