Reputation: 67
If the form/app (here and further in the text talking about I have created my own a form/application in C# using WinForms) for some reason has not closed by the user, for example the PC abruptly passed out or antivirus app close my app, in this case, when the application closes, I need in the this same application created the file to which you wrote the reason for the app shutdown. I had an idea to create a file in the FormClosing event handlers or in FormClosed to write the reason in (FormClosingEventArgs)e.CloseReason, but it didn't work... I decided to test it through the KillProcess program (I use this program, because if, for example, the I stop the application through the Windows Task Manager, then the file will be written that the user stopped the application himself, so I needed software so that my application did not determine that I closed it), which closing the program, but my application does not even have time to blink, not that it would create a file and write data there about the reason for termination, in general, it does not create or write anything. What are the options for implementing this task? I need one app to do all this, that is, without additional apps that analyze my app for.
Code with a handler:
private void UserForm_FormClosing(object sender, FormClosingEventArgs e)
{
Console.WriteLine("Closing!!!");
string path = @"C:\Users\Kulic\Desktop\reasonclose.txt";
FileInfo fileInf = new FileInfo(path);
switch (e.CloseReason)
{
case CloseReason.None:
{
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("The reason for the closure was not determined or cannot be determined.");
}
}
break;
}
case CloseReason.WindowsShutDown:
{
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("The operating system closes all applications before shutting down.");
}
}
break;
}
case CloseReason.MdiFormClosing:
{
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("The parent form of this multi-document interface (MDI) form is closed.");
}
}
break;
}
case CloseReason.UserClosing:
{
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("The form is closed programmatically or through user action in the UI (such as clicking the Close button in the window forms, select Close in the system menu of the window or by pressing ALT+F4).");
}
}
break;
}
case CloseReason.TaskManagerClosing:
{
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Microsoft Windows task Manager closes the application.");
}
}
break;
}
case CloseReason.FormOwnerClosing:
{
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("The owner form is closed.");
}
}
break;
}
case CloseReason.ApplicationExitCall:
{
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("The Exit() method of the Application class was called.");
}
}
break;
}
default:
{
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("The reason for closing was not revealed");
}
}
break;
}
}
}
Upvotes: 0
Views: 1033
Reputation: 833
You can also try subscribing to the AppDomain.ProcessExit
event (documentation), but keep in mind that even that is not guaranteed to fire in all cases. It all depends on how your program was killed.
For example, if the computer unexpectedly loses power or crashes, then nothing will run after that, and there is no way to save the closing reason to a file.
Also, this is out of the scope of your question, but that code is way over complicated. You only need to check File.Exists
and open the stream once. You can put the switch statement inside the using
block.
Upvotes: 0
Reputation: 7813
The FormClosing
event only has a chance of firing when the program or form is closed though normal means. Using the task manager to kill the process (or anything else that abruptly closes it) simply skip it and any other shutdown code.
This is not a bug on your code, but a result on the very nature of process kill: stop the thing right now, no matter what. On this situation, nothing even has a chance to run, because your process is killed. Imagine a situation where you just unplug the computer and it goes off, nothing can shut down correctly.
There is no way to catch such an event. You can get normal closure reasons, but not an abnormal one.
Upvotes: 2