Friday, February 11, 2011

WinForm Application Prevents Machine Restart?

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;
        }
    }
  • Have you spawned threads in your app and are they running? If so, make sure they're set to IsBackground = true.

    Soo : Will backgroundworkers already have IsBackground = true?
  • 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);
        }
    
    Soo : I have something like this so it must be blocking the close. How can I get around this?
    Hans Passant : I reckon it is not quite something like this. I cannot guess, post a snippet of your code in your question.
    Soo : I put it up there
    Hans Passant : @Soo: where are you testing the e.CloseReason value in that code?
    Soo : CloseReason.UserClosing is only if the user is trying to close the app, not the computer?
    Hans Passant : Right, only if the user is closing the window do you want to cancel it.

0 comments:

Post a Comment