Sleep (suspend) and Resuming window form run the program on the workflow instead of the main thread

In the form window, I work on subscriptions to Microsoft.Win32.SystemEvents.PowerModeChanged, and on Suspendit runs a method Close()on the form. In Resumeit starts the function Run(), as at boot. The problem is that when the computer wakes up from sleep mode, the PowerModeChanged event is fired on a workflow named ".Net SystemEvents", and when Run()called, it recreates the form in this workflow instead of the main thread.

This form is a project that I inherited from another developer, and I'm new to window programming. I am wondering if there is a better way to handle the sleep / wake process, rather than closing the forms and re-creating them upon waking, or a way to force the creation to the main thread. Note. If I delete the code and do nothing when the computer is asleep (paused) and nothing wakes up (resumes), the program continues to work correctly (it works in the main thread). Any help would be appreciated. Thanks to everyone.

+2
source share
1 answer

Capture the SynchronizationContext UI thread in and Post()on a static field to return to the UI thread.

+4
source

All Articles