Monopompom
Monopompom

Reputation: 161

How to check thread before closing program?

Well, I have a main thread and one more additional. Now I need to check additional thread before closing the program and if it's running - ask 'terminate it or not', else - just close the app.

1st edit:

 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (calculation.IsAlive)
        {
            e.Cancel = true;
        }
        else
        {
            //
        }
    }

How to close app. in 'else' part? I think it's so easy but I can not find out it))

Upvotes: 0

Views: 147

Answers (2)

S.N
S.N

Reputation: 5140

Thread.IsAlive flag will do the trick.

   var t=new Thread(() => { });
            if (t.IsAlive)
            {
                //thread is still alive
                //true if this thread has been started and has not terminated normally or aborted;
            }
            else
            {
                //not alive
                //otherwise, false.
            }

Upvotes: 2

AKD
AKD

Reputation: 3966

Check Thread.IsAlive() to see if the thread is still running.

Upvotes: 3

Related Questions