Jason94
Jason94

Reputation: 13610

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?

Upvotes: 5

Views: 1283

Answers (1)

Armen Tsirunyan
Armen Tsirunyan

Reputation: 133014

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

Upvotes: 2

Related Questions