0

I am created a class inherited from ApplicationContext in .NET and I'm wondering where is best to raise my OnClosing event. Now, ApplicationContext has a "ThreadExit" event, but this fires after both ExitThread and ExitThreadCore have run. Is this where a typical OnClosing event would go? This seems more like where a OnClosed event would go.

As a follow up, where should my OnClosed event go? overriding Dispose and putting it at the beginning seems plausible I suppose. Would the end of Dispose be safe? Or might the OnClosed even need something that was just deleted?

Edit: So it looks like I have to use the ThreadExit event inherited from ApplicationContext. My question now would be, is it safe to cancel the closing from here? Also still, where should I fire my OnClosed from?

user912447
  • 696
  • 1
  • 11
  • 31
  • Hard to make sense of this, the ApplicationContext class models the state of the UI thread. As long as there is one active, the UI thread is pumping the message loop. OnClosing makes little sense, there's no point in canceling it when there are no windows left, nothing much is going to happen after the last window is gone. ThreadExit already signals the end of the thread. Maybe ThreadExiting, but what are you going to do about it and how is it different from ThreadExit immediately following it. – Hans Passant Jun 26 '12 at 23:19
  • That is assuming you have windows. My class very well may not be using any windows at the time of it's closing. It is a class that is modeling an app running from the system tray. – user912447 Jun 27 '12 at 16:39
  • Does ThreadExit work similar to, say, OnFormClosing with a Forms application? IF so I can just use it instead of creating a new one, though I'm not a huge fan of that name. – user912447 Jun 27 '12 at 16:56
  • 1
    http://stackoverflow.com/questions/1730731/how-to-start-winform-app-minimized-to-tray/1732294#1732294 – Hans Passant Jun 27 '12 at 17:02
  • That is still, fundamentally, a Form application. Everything is contained in the form class and spawns from it. I specifically do not want a Form controlling a taskbar app. Personally, I just don't think it makes sense to have the core of your application (one running in the background and represented in the System Tray) controlled by an extraneous class (Form) that isn't even required for operation. As a result of this design idea I am not using Application.Run(new Form1()); but instead: Application.Run(new myApplicationContext()); – user912447 Jun 27 '12 at 17:43

1 Answers1

0

I will go with using ThreadExit event, though I don't like that name. I am still wondering the best place to put my OnClosed event however.

user912447
  • 696
  • 1
  • 11
  • 31