D
Dan Tallent
I am trying to learn how to create an application that uses worker threads
to prevent the UI from freezing up.
My question is: Is the following code considered "thread safe"?
If not, what am I doing wrong?
Thanks
Dan
--------------------------------------------------------
I have a form with this code:
--------------------------------------------------------
private void button1_Click(object sender, System.EventArgs e)
{
WorkerClass wc = new WorkerClass();
wc.ProcessComplete+= new WorkerClass.ProcessCompleteHandler(WorkerDone);
wc.Process();
Console.WriteLine("Waiting for worker thread");
}
public void WorkerDone(long current)
{
Console.WriteLine("main: Worker thread complete" + current.ToString());
this.textBox1.Text = current.ToString();
}
--------------------------------------------------------
A also have a seperate class with this code:
--------------------------------------------------------
using System;
using System.Threading;
namespace TestWorkerThread
{
/// <summary>
/// Summary description for WorkerClass.
/// </summary>
public class WorkerClass
{
long Result;
public delegate void ProcessCompleteHandler(long current);
public event ProcessCompleteHandler ProcessComplete;
public WorkerClass()
{
//
// TODO: Add constructor logic here
//
}
public void Process()
{
// Create a new thread and call a function on that new thread.
// This method should return without waiting
ThreadStart myThreadDelegate = new ThreadStart(ProcessData);
Thread myThread = new Thread(myThreadDelegate);
myThread.Start();
}
private void ProcessData()
{
Result = 0;
for (int x = 0; x<10; x++)
{
Result = Result + x;
Thread.Sleep(1000);
}
// Fire the event signal. Any subscribers will then know the processing is
done.
OnProcessComplete(Result);
}
protected void OnProcessComplete(long current)
{
ProcessComplete(current);
}
}
}
----------------------------------------------------------------
to prevent the UI from freezing up.
My question is: Is the following code considered "thread safe"?
If not, what am I doing wrong?
Thanks
Dan
--------------------------------------------------------
I have a form with this code:
--------------------------------------------------------
private void button1_Click(object sender, System.EventArgs e)
{
WorkerClass wc = new WorkerClass();
wc.ProcessComplete+= new WorkerClass.ProcessCompleteHandler(WorkerDone);
wc.Process();
Console.WriteLine("Waiting for worker thread");
}
public void WorkerDone(long current)
{
Console.WriteLine("main: Worker thread complete" + current.ToString());
this.textBox1.Text = current.ToString();
}
--------------------------------------------------------
A also have a seperate class with this code:
--------------------------------------------------------
using System;
using System.Threading;
namespace TestWorkerThread
{
/// <summary>
/// Summary description for WorkerClass.
/// </summary>
public class WorkerClass
{
long Result;
public delegate void ProcessCompleteHandler(long current);
public event ProcessCompleteHandler ProcessComplete;
public WorkerClass()
{
//
// TODO: Add constructor logic here
//
}
public void Process()
{
// Create a new thread and call a function on that new thread.
// This method should return without waiting
ThreadStart myThreadDelegate = new ThreadStart(ProcessData);
Thread myThread = new Thread(myThreadDelegate);
myThread.Start();
}
private void ProcessData()
{
Result = 0;
for (int x = 0; x<10; x++)
{
Result = Result + x;
Thread.Sleep(1000);
}
// Fire the event signal. Any subscribers will then know the processing is
done.
OnProcessComplete(Result);
}
protected void OnProcessComplete(long current)
{
ProcessComplete(current);
}
}
}
----------------------------------------------------------------