Home
Manage Your Code
Snippet: EasyThread class (C#)
Title: EasyThread class Language: C#
Description: Quick and easy threading in C# Views: 712
Author: James Taylor Date Added: 7/3/2007
Copy Code  
1//just inherit from the EasyThread class and override the PerformWork method

2
3public class EasyThread: IDisposable
4{
5Thread WorkerThread;
6public EasyThread()
7{
8if (WorkerThread == null)
9WorkerThread = new Thread(new ThreadStart(PerformWork));
10}
11
12
13public void Run()
14{
15if (WorkerThread.IsAlive == false)
16WorkerThread.Start();
17
18if (WorkerThread.ThreadState == ThreadState.Suspended)
19WorkerThread.Resume();
20}
21
22/// <summary>

23/// EasyThread provides a facade to inheriting from a Thread class.

24/// Override the perform work method to perform your tasks.

25/// </summary>

26protected virtual void PerformWork()
27{
28
29}
30
31public void Pause()
32{
33WorkerThread.Suspend();
34}
35
36public void Quit()
37{
38Cleanup();
39}
40
41private void Cleanup()
42{
43WorkerThread.Join(0);
44WorkerThread = null;
45}
46
47public void Dispose()
48{
49Cleanup();
50}
51}
Notes
Just inherit from the EasyThread class and override the PerformWork method