Question with thread in C #

My existing C # application is updating the sharepoint site collection. Now I need to make an update for the C # application, not 1, but 15 site collections. It takes too long to work sequentially, so threads will be a good solution.

However, I can not immediately open only 15 threads, I would like to have a custom value that will be the maximum number of threads to start immediately.

What would I expect:

  • Define the variable Max_Threads ...
  • Assign a queue of tasks (functions) in the thread pool, in this case 15 tasks or function calls.
  • Then the thread pool executes each thread, however it is limited by the number of open threads (for example, 5).
  • Once a thread completes, threadpool then reuses that thread to perform another work item until all work is complete.

So my question is: all this is embedded in .net-threading. Or do I need to manually create the flow control code?

EDIT: this is a draft framework 3.5, sorry for not mentioning this before.

+4
source share
5 answers

Use TPL , which in turn will use the .NET thread pool (default). It is supposed to automatically configure the environment in which it works:

Starting with the .NET Framework version 4, the default size of the thread pool for a process depends on several factors, such as the size of the virtual address space.

You only need to perform your own flow control in a very limited number of cases :

  • You need a front end.

  • A stream requires a certain priority.

  • You have tasks that make the thread block long periods of time. A thread pool has a maximum number of threads, so a large number of blocked threads of a thread pool can prevent tasks from starting.

  • You need to put streams in a single-threaded apartment. All ThreadPool Threads are in a multi-threaded apartment.

  • You need to have a strong identity associated with the flow, or dedicate the flow to the task.

If you are using .NET 3.5, you should look at an article that explains how to get some TPL functions using a thread pool directly: Does TPL enable you to do something that you could not do with ThreadPool in .NET 2.0? .

+1
source

just use Tasks and don't think about thread numbers. TPL will do all this for you.

+3
source

The C # book in a nutshell, 4.0 (by Albahari and Albahari) provides a neat example of calling many "web clients" and downloading information at the same time. It uses PLINQ, does it in one or more lines of code. The PLINQ library handles thread creation, etc. If you look at it, it has no tendency to create too many threads, although you can limit the maximum number of threads at a time. Similarly, you can use Parallel.Foreach () and use the ParallelOptions parameter to limit the number of threads.

The best part is that you never need to create threads yourself - this is automatic. And it copes with load balancing.

A good tutorial is http://www.albahari.com/threading/ - take a look at Part 5 on parallel programming for many examples of using PLINQ and Parallel.Foreach.

In addition, Wagner’s book, Effective C # (4.0)) has similar examples.

+3
source

You can use the Parallel framework that comes with .NET 4.0: http://msdn.microsoft.com/en-us/library/dd460693.aspx

+2
source

Consider using Reactive Extensions backport for .NET 3.5. This will allow you to use the parallel task library (TPL). If this is not an option, continue reading.

I would immediately throw your entire site into ThreadPool and see how it works before trying to throttle work items.

If you feel that you need to throttle the number of simultaneous work items in ThreadPool , then you can use the semaphore to throttle them. However, be careful not to block the ThreadPool thread, as this is considered bad practice. Instead, block the queue flow.

 int pending = sites.Count; var finished = new ManualResetEvent(false); var semaphore = new Semaphore(5, 5); foreach (string site in sites) { semaphore.WaitOne(); ThreadPool.QueueUserWorkItem( (state) => { try { // Process your work item here. } finally { semaphore.Release(); if (Interlocked.Decrement(ref pending) == 0) { finished.Set(); // This is the last work item. } } }, null); } finished.WaitOne(); // Wait for all work items to complete. 
+1
source

All Articles