I have a windows form application which is writing bunch of stuff in the database. Now some time ago I was asking for the solution of how to be sure that all requests (writing to the database) has been done before windows form is closed.
The reason why am I doing this is because it will be a self standing application which has an automatic shut down and therefore I need to be able to finish all my writing and reading from the database and be sure when I am closing form that all the job is done.
Before I tried this solution I had a problem of cutting a connection when writing in the database because of a form closing.
Now the problem is that when I have a shut down process my windows form application won't close, so after some time it just cuts of the electricity which is not what I need.
Here is the code, and please tell me what is wrong with it? I need to close the form when program is sure that no task is working in the background.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// this.Visible = false; // optional
// this.ShowInTaskbar = false; // optional
Task db = Task.Factory.StartNew(() => DBUpdate());
Task.WaitAll(db);
if (this.shutdownRequested)
Process.Start("shutdown.exe", "-s");
}
protected override void WndProc(ref Message ex)
{
if (ex.Msg == WM_QUERYENDSESSION)
{
Message MyMsg = new Message() { Msg = WM_CANCELMODE };
base.WndProc(ref MyMsg);
this.shutdownRequested = true;
} else {
base.WndProc(ref ex);
}
}
EDIT:
I just realized that this is working when I click the X button and form starts to close, but when pc goes into the shutdown phase then its not. Any idea? Maybe I need to change a event on something else and not on the FormClosing ? One more thing. I have saved a priority of the application process as a real-time process.