Let us see how we can add a new thread with an event to the thread pool. Thus, this method adds a new thread with a triggering event. When the wait operation completes, a worker thread from the pool executes the corresponding callback function. There is one thread that constantly observes the status of the wait operations queued to the thread pool. The modus operandi of this is very simple. This mechanism, involving the ThreadPool and an event based programming pattern, facilitates the ThreadPool to monitor all the WaitHandles registered with it and then calling the appropriate WaitOrTimerCallback delegate method when the WaitHandle is released. You can also queue tasks by calling ThreadPool.RegisterWaitForSingleObject method and passing a that when signaled or when timed out raises a call to a method wrapped by the delegate. ThreadPool.QueueUserWorkItem (m圜allback) add this to the thread pool / queue a task WaitCallback m圜allback = new WaitCallback (afunction) do what ever the function is supposed to do. Just create a delegate of type WaitCallback and add it to thread pool. The following code shows how you can create a thread pool. The runtime automatically creates threads for queued thread pool tasks and then releases them when the task is done. This method takes as a parameter a WaitCallback delegate (wrapping the object that you want to add to the queue) that will be called by the thread selected from the thread pool. This method adds threads to be executed when the processor is free. You request the thread pool to handle a task (or work items) by calling the QueueUserWorkItem method of the ThreadPool class. Once a work item has been queued to the thread pool, you cannot cancel it. Each thread runs at the default priority and uses the default stack size and is in the multi-threaded apartment. The number of operations that can be queued to the thread pool is limited only by the available memory. If you create threads that are in addition to the limit specified, then those threads can start only once the currently running threads stop. The only restriction being that the number of worker threads created will never exceed the maximum number allowed. The thread pool continues creating worker threads and allocates tasks which are pending in the queue. If one of the threads is waiting on an event, the thread pool will initiate another worker thread on the processor. Thus, the processor is never made to sit idle. It has a default limit of 25 threads per available processor, but that can be changed. The thread pool is created the first time when you instantiate the ThreadPool class. This class provides your application with a pool of worker threads that are managed by the system thus allowing you to concentrate on the workflow logic rather than thread management. But remember that at any particular point of time there is only one thread pool per process and there is only one working thread per thread pool object. Using the ThreadPool class enables the system to optimize thread time-slices on the processor. Thread pooling is a form of multithreading where tasks are added to a queue and automatically started as threads are created. If there are a number of tasks, each requiring one thread, then you should consider using the ThreadPool class (in the System.Threading namespace) to manage your resources efficiently and to take benefits of multiple threads. No doubt, as you would also agree, this is an absolute waste of resources. Async method invocation using delegates are also a preferred threading mechanismĪs you start creating your own multi-threaded applications, you would realize that for a large part of your time, your threads are sitting idle waiting for something to happen (like a key press or listening for requests at the socket etc). The Timer class is a flexible way to run tasks at specified intervals. It provides an efficient thread management mechanism to handle multiple tasks. For large number of threads, it is always better to consider thread pooling. Using the Thread class to create and manage threads is ideal for situations when the number of threads is small and you want to control the thread details like the thread priority etc.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |