Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,
I have one button and when the button.text equals "start" I am calling my method startThread() where a thread is started. Otherwise my mehtod stopthread() gets called where I set a boolean variable to false. This scenario only works once. If I click start again the following error is shown:

"Thread is running or terminated; it cannot restart."


How do I handle this?

Thanks in advance!
Posted
Updated 3-Aug-11 1:24am
v2
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 3:27am    
How about a ***simple*** code sample demonstrating the problem? Use "Improve question" above.
--SA
Sergey Alexandrovich Kryukov 3-Aug-11 3:28am    
What is "MultiThread"? There is no "startThread()" or "stopthread()" in .NET. How all this is related to SQL?
--SA

A thread may be suspended and resumed, but once a thread has stopped it can not be brought back to life. Your best bet would be either to use ThreadPool or create a new thread when the button is meant to start the thread. If I were you I'd create a new thread in the method called startThread().

Trying to restart a thread that has already stopped will lead to exactly the error you have witnessed.
After reading again what the other posters suggested I'd say you stick to what OriginalGriff[^] proposed in his answer[^].

Best Regards,

—MRB
 
Share this answer
 
v4
Comments
#realJSOP 3-Aug-11 7:32am    
5 - proposed as answer, with a small correction. Once the thread has stopped or been aborted, it's not just good advice to, but your are REQUIRED to create a new instance of the thread.
Manfred Rudolf Bihy 3-Aug-11 8:14am    
Thanks John!
Akshatha.A 3-Aug-11 7:54am    
Thanks..... i will try
AFAIK, SQL does not support threading. But, the principle is pretty simple. This example uses a BackgroundWorker - which is probably all you will need for this kind of task:
C#
BackgroundWorker bw;
private void button1_Click(object sender, EventArgs e)
    {
    Button b = sender as Button;
    if (b != null && b.Text == "Start")
        {
        bw = new BackgroundWorker();
        bw.WorkerSupportsCancellation = true;
        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.RunWorkerAsync();
        b.Text = "Stop";
        }
    else
        {
        if (bw.IsBusy)
            {
            bw.CancelAsync();
            }
        b.Text = "Start";
        }
    }
void bw_DoWork(object sender, DoWorkEventArgs e)
    {
    BackgroundWorker worker = sender as BackgroundWorker;
    int i = 0;
    while (true)
        {
        if (worker.CancellationPending == true)
            {
            e.Cancel = true;
            break;
            }
        else
            {
            Console.WriteLine(i++);
            System.Threading.Thread.Sleep(500);
            }
        }
    }
 
Share this answer
 
Comments
Manfred Rudolf Bihy 3-Aug-11 7:28am    
Exactly what OP was looking for I'd say! 5+
Proposed as solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900