Click here to Skip to main content
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 22:09pm
Edited 3-Aug-11 2: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 OriginalGriff 390
1 Sergey Alexandrovich Kryukov 329
2 Afzaal Ahmad Zeeshan 244
3 BillWoodruff 210
4 Maciej Los 192
0 OriginalGriff 5,560
1 DamithSL 4,476
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 3 Aug 2011
Copyright © CodeProject, 1999-2014
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