views:

41

answers:

2

I have a WinForm application, and when it's open and I try to restart my computer, my computer just hangs and doesn't restart. I have to actually close the WinForm app, and then restart, and my computer restarts.

What can I do to solve this?

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (!BoolShouldThisClose)
        {
            e.Cancel = true;
            this.Visible = false;
        }
    }
A: 

Have you spawned threads in your app and are they running? If so, make sure they're set to IsBackground = true.

Judah Himango
Will backgroundworkers already have IsBackground = true?
Soo
+1  A: 

Be sure to pay attention to the CloseReason so you won't block Windows trying to close your form. Like this:

    protected override void OnFormClosing(FormClosingEventArgs e) {
        if (e.CloseReason == CloseReason.UserClosing) {
            this.Hide();
            e.Cancel = true;
        }
        else base.OnFormClosing(e);
    }
Hans Passant
I have something like this so it must be blocking the close. How can I get around this?
Soo
I reckon it is not quite something like this. I cannot guess, post a snippet of your code in your question.
Hans Passant
I put it up there
Soo
@Soo: where are you testing the e.CloseReason value in that code?
Hans Passant
CloseReason.UserClosing is only if the user is trying to close the app, not the computer?
Soo
Right, only if the user is closing the window do you want to cancel it.
Hans Passant