Background |
public class BackgroundThreadsManager : IDisposable
The BackgroundThreadsManager type exposes the following members.
Name | Description | |
---|---|---|
BackgroundThreadsManager | Constructor |
Name | Description | |
---|---|---|
ThreadAbortDelayMs | Gets or sets a integer value that specifies the number of milliseconds that are used to delay the call to Thread.Abort method after the thread is not used any more. This is used to prevent creation and abortion of threads when the number of used threads is changing rapidly because the number of rendered objects is also changing. Default value is 1000 and delays the thread abortion for 1 second. Zero value means that the unused threads are immediately aborted. To immediately abort unused threads, set ThreadAbortDelayMs to 0 and then call SetupThreads(Int32) method. | |
ThreadsCount | Gets the number of currently active background threads (including the threads that are schedules to be aborted). |
Name | Description | |
---|---|---|
Dispose | Dispose | |
Execute | Execute method executes the specified taskAction on all threads that were before set up with calling the SetupThreads(Int32) method. The taskAction gets a thread index as parameter. | |
GetException | GetException returns an Exception object for the thread with the specified index. If no exception was thrown by this thread, then null is returned. | |
HasExceptions | HasExceptions returns true if any executed action has thrown an exception. | |
SetupThreads | SetupThreads sets up the background threads based on the specified threadsCount. When the number of currently created background threads is bigger then the specified threadsCount, the unused threads are scheduled to be aborted (see ThreadAbortDelayMs). To immediately abort unused threads set ThreadAbortDelayMs to 0 before calling this method. | |
WaitAll | Waits indefinitely until all taskActions that were passed to the Execute(BackgroundTaskStartDelegate) method are completed. | |
WaitAll(Int32) | Waits until all taskActions that were passed to the Execute(BackgroundTaskStartDelegate) method are completed. If the this takes longer than the millisecondsTimeout, the methods returns with false value. |
BackgroundThreadsManager can be used to create multiple threads and execute custom actions on them.
To set up the number of required threads call the SetupThreads(Int32) method. Then you can call the Execute(BackgroundTaskStartDelegate) method to executed the specified action on all set up threads. When number of threads in the call to the SetupThreads(Int32) method is lower then the number specified in the previous call, the unused threads are not immediately aborted but instead they are scheduled to be aborted after the ThreadAbortDelayMs time.
You can derive your own class from BackgroundThreadsManager and override the SetupThreads, Execute, WaitAll and Dispose methods to provide you own threading mechanism.
IMPORTANT: The methods in this class are not thread safe and must be called only from one thread.