I am running a thread like this:
instance variable
private Thread workerThread = null;
in contructor
workerThread = new Thread(new ThreadStart(this.remoteRequestBackgroundTask));
in a method, I start the thread
private void btnTransferData_Click(object sender, EventArgs e)
{
////Start the thread
workerThread.Start();
//remoteRequestBackgroundTask();
}
remoteRequestBackgroundTask
private void remoteRequestBackgroundTask()
{
//does some background processing and then finishes
}
This works the first time I run the Thread, however, when it finishes and I run it again, I get “System.Threading.ThreadStateException”. How can I fix this?
Do not create the worker thread in the constructor. You should move this code into the btnTransferData_Click method.