Click here to Skip to main content
11,717,914 members (80,224 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Threading
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 2-Aug-11 21:09pm
Edited 3-Aug-11 1:24am
v2
Comments
SAKryukov at 3-Aug-11 3:27am
   
How about a ***simple*** code sample demonstrating the problem? Use "Improve question" above.
--SA
SAKryukov at 3-Aug-11 3:28am
   
What is "MultiThread"? There is no "startThread()" or "stopthread()" in .NET. How all this is related to SQL?
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
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);
            }
        }
    }
  Permalink  
Comments
Manfred R. Bihy at 3-Aug-11 7:28am
   
Exactly what OP was looking for I'd say! 5+
Proposed as solution.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v4
Comments
   
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 R. Bihy at 3-Aug-11 8:14am
   
Thanks John!
Akshatha.A at 3-Aug-11 7:54am
   
Thanks..... i will try

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 849
1 OriginalGriff 215
2 F-ES Sitecore 210
3 Maciej Los 140
4 Jochen Arndt 120
0 Sergey Alexandrovich Kryukov 1,494
1 OriginalGriff 535
2 Maciej Los 529
3 Richard MacCutchan 440
4 CHill60 415


Advertise | Privacy | Mobile
Web02 | 2.8.150901.1 | Last Updated 3 Aug 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100