0

As part of my work I am trying to do some processing when the OS is shutdown. To do this, I found this resource.

It supposed to explain how to do this but for events like a OS shutdown it uses Debug.Writeline.

But how can you see the Debug in your visual studio if the system is shutting down? I can not imagine how.

Naively I tried, and visual studio gave me an unrecoverable error


The resource is the book C# Cookbook and the recipe 19.1 Dealing with Operating System Shutdown, Power management or user session changes. They suggest to implement a function public static void RegisterForSystemEvents() (using Win32.SystemEvents) where you will hook up event handlers to capture these events for example

SystemEvents.SessionEnding+= new SessionEndingEventHandler(OnSessionEnding)

and inside these they put Debug.WriteLine as in

private static void OnSessionEnding(object sender, SessionEndingEventArgs e)
{
e.Cancel=false;
Debug.WriteLine("some message here");
}

The thing is how can you see these debug messages if the OS is shutting down?

KansaiRobot
  • 5,148
  • 7
  • 44
  • 95
  • 1
    That resource is not easily accessible. Please put all relevant info in the question. – mjwills Feb 13 '18 at 09:48
  • It's an **example**. You wouldn't use that for an actual app. – stuartd Feb 13 '18 at 09:55
  • Of course. I am asking how (or if) to run the example – KansaiRobot Feb 13 '18 at 12:50
  • @mjwills apologies. Will do tomorrow when at the working pc again – KansaiRobot Feb 13 '18 at 12:50
  • Maybe debug during real OS shutdown is not a perfect. Why not simulate windows shutdown? https://stackoverflow.com/questions/520910/how-to-simulate-windows-shutdown-for-debugging – Fletcher Feb 14 '18 at 02:24
  • @FletchZhou-MSFT Unfortunately I can't understand that answer at all. The accepted answer uses a third party that apparently does not come with windows anymore.. – KansaiRobot Feb 14 '18 at 05:57
  • @KansaiRobot, Remote debugging tool or visit the thread windows would be a workaround for you. But why you still want to get the output messages if your OS is closed, maybe you could add the if..else..., and then just output different messages during the OS is closed or not closed for you. If I misunderstood it, feel free to let me know. – Fletcher Feb 15 '18 at 08:55

0 Answers0