tags:

views:

49

answers:

1

When a form is closed, it gets a useful argument in its FormClosed event, CloseReason. However our app runs with only a tray icon and no windows open most of the time. When it exits, I'd like to log the reason it exited - in particular, knowing CloseReason.WindowsShutDown and CloseReason.TaskManagerClosing would be useful.

Is there any way to check for the above after Application.Run returns, in the absence of any Form instances?

A: 

There is no built-in way of doing this, but you could always do your own.

The easiest would probably be to add a static property to your Program class where you assign the CloseReason you get in the FormClosed event. This could then be read in your code at a later point.

Alternatively you could create your own class that derives from ApplicationContext and handle the FormClosed event there. That might be a cleaner approach, but it would also be more cumbersome to set up.

Rune Grimstad
The former idea won't work because there are no forms to be closed. Looking at whether ApplicationContext can do this, thanks for the link.
romkyns
ApplicationContext can't do this either. Normally I would -1, but that seems unreasonable since learning about ApplicationContext was still handy...
romkyns