source from:http://www.codeproject.com/KB/cs/workerthread.aspx
后台处理类
public class BackWorker
{
public delegate void DelegateAddString(String s);
public delegate void DelegateThreadFinished();
public DelegateAddString m_DelegateAddString;
public DelegateThreadFinished m_DelegateThreadFinished;
// Main thread sets this event to stop worker thread:
public ManualResetEvent m_EventStopThread;
// Worker thread sets this event when it is stopped:
public ManualResetEvent m_EventThreadStopped;
public BackWorker()
{
// initialize events
m_EventStopThread = new ManualResetEvent(false);
m_EventThreadStopped = new ManualResetEvent(false);
}
public void Start()
{
int i;
String s;
for (i = 1; i <= 101; i++)
{
// make step
s = "Step number " + i.ToString() + " executed";
Thread.Sleep(1000);
// Make synchronous call to main form.
// MainForm.AddString function runs in main thread.
// To make asynchronous call use BeginInvoke
//m_form.Invoke(m_form.m_DelegateAddString, new Object[] { s });
this.m_DelegateAddString(s);
// check if thread is cancelled
if (m_EventStopThread.WaitOne(0, true))
{
// clean-up operations may be placed here
// ...
// inform main thread that this thread stopped
m_EventThreadStopped.Set();
&