3

I have this C# app which starts at system boot in the tray, and i have the following problem with it, only on Windows XP

I can't restart the PC while the application is running. If I use file > exit, it stops ok and then i can restart. but if i try restarting with the application open, it just won't do it

I tried adding this in the main window constructor, dunno if its the right thing to do:

Application.ApplicationExit += new EventHandler(this.OnApplicationExit);

and the OnApplicationExit function does the app's shutting down procedure.. but that doesn't help

any ideas?

Andrei S
  • 6,486
  • 5
  • 37
  • 54
  • This is most likely because the GUI thread is busy doing something. What do you do in the application? – Aliostad Oct 07 '10 at 12:23
  • How do you start your application? by command prompt or throught UI or something else? – Shekhar Oct 07 '10 at 12:24
  • @Joey: I would avoid saying that you should use `TerminateProcess`. It's more of a last resort which you should only use if you really have to since it doesn't allow the process to shut down in an orderly fashion. – Hans Olsson Oct 07 '10 at 12:30
  • @ho1: I was referring to what Windows does (afaik) when shutting down. Not as an advice what to do yourself. – Joey Oct 07 '10 at 15:39
  • @joey: Sorry, I probably misunderstood your previous comment (I can't remember what it said). – Hans Olsson Oct 07 '10 at 19:05

2 Answers2

4

Do you have a FormClosing event handler somewhere where you do something like e.Cancel = true;?

If so, change it to first look at the close reason to decide if it should cancel or not as:

if(e.CloseReason != WindowsShutDown)
     e.Cancel = true;

There might be other CloseReasons where you should also not Cancel the closing so might be worth looking at MSDN for that.

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • That seemed to solve my problem. I would have never imagine my application could block the entire system. Thank you! – Andrei S Oct 07 '10 at 12:51
1

I've seen this happen before if you've got Cancel = true somewhere in your exit handler.

Fidel
  • 7,027
  • 11
  • 57
  • 81