Need to run a function just before the syatem shutdown in C# Windows application

Sachin Kumar 1 Reputation point
2023-01-09T23:50:51.54+00:00

I am working on c# windows application. I have a requirement to do to run a certain task if system shutdown or restarts.
Please suggest if we can trap the system shutdown event just before happening?

Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-01-10T07:48:51.907+00:00

    Hi @Sachin Kumar ,Welcome to Q&A.

    This is possible, you need to refer to WM_QUERYENDSESSION message and SystemEvents.SessionEnding Event.

    You can also Use OnShutdown to specify the processing that occurs when the system shuts down.

    This following case can give a good example of your problem.

    Best Regards, Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.