Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting a stopped thread?

I've made a combined start/stop button for starting and stopping a thread.

Looks like this:

private void btStartStop_Click(object sender, EventArgs e)
        {
            if (btStartStop.Text.Equals("Start"))
            {
                btStartStop.Text = "Stop";
                stopThread = false;

                ThreadState ts = thread.ThreadState;

                if (thread.ThreadState == ThreadState.Stopped)
                    thread = new Thread(DoWork);

                thread.Start();
            }
            else
            {
                btStartStop.Text = "Start";
                stopThread = true;
                thread.Join();
            }
        }

Can i check the threadstate like i do and if stopped init it again? or is it some other way since i cant just start a thread if its stopped?

like image 570
Jason94 Avatar asked Nov 05 '22 17:11

Jason94


1 Answers

Yes, you can. You are reusing only the reference to a thread object, setting it to a completely new thread object.

like image 62
Armen Tsirunyan Avatar answered Nov 09 '22 16:11

Armen Tsirunyan