Answer the question
In order to leave comments, you need to log in
How to understand for what reason the form was closed?
If the form/application (hereinafter referred to as the form/application I created in C# via WinForms) for some reason was closed by a non-user, for example, the PC suddenly passed out or the anti-virus program shut down the application, in this case, after closing the application, it is necessary that the same application creates a file in which it writes the reason for shutting down the work.
There was an idea to create a file in the FormClosing or FormClosed event handlers in which the reason will be written through (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, with handles stop the application through the task manager, then it will be written to the file that the user himself stopped the application, so I needed software so that my application would not determine that I had closed it), which terminates the program, but my application does not even have time to blink, does not what would create a file and write there data about the reason for the end, in general, does not create or write anything.
What are the options for this task? It is necessary that all this be done by one application, that is, without additional. applications that analyze my application.
Handler code:
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;
}
}
}
Answer the question
In order to leave comments, you need to log in
Reminds "that's when they kill - come")
According to known signals and regular closures - write "closed in ...", all other situations should be interpreted as "unforeseen completion".
Try this technique. In theory, you can do even more bells and whistles, but it will be more difficult
. How to get the exit code of the program?
Request is clearly not set, and resharper should be installed)
in the description of the errors, as if everything is already written what needs to be done ..
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question