I want to close the form displayed in C#dll during shutdown

Asked 2 years ago, Updated 2 years ago, 46 views

Thank you for your help.

Generally, if you log off or shut down a form even if it's up,
I'm going to close it together, but I'm going to set the application for the form that I'm going to launch as dll and
Launch a dllized form referenced from another app to log off and
When shut down, the parent form is closed properly, but dllized
The app doesn't close and
like
This app is preventing you from restarting. A message appears and the shutdown stops.

Therefore, we will look it up online and implement the following events to shut down and log off
When I tried to, I tried to close the form together, but it still didn't work.

// Logoff, attempting to shut down
private void SystemEvents_SessionEnding (object sender, SessionEndingEventArgse)
{
        Application.Exit();
}

By the way, the dllized form is an app that made digital watches, and
in Timer. It's a specification that redraws the form every second, but what is the cause?
If this form remains a single exe, it will close properly during shutdown.

Could you tell me the cause and countermeasures?

Thank you for your cooperation.

Sorry for the postscript.It seems that the way to find the cause was too lenient.
When I tried it, the cause was not the person who turned it into dll, but the parent app who called it.
Once you start the form from scratch and call the dll form of the clock to it, it will be safe
Close with shutdown, but dll empty form and
in existing parent app When I called it, it prevented me from restarting just like I did when I was clocking.
There seems to be no problem with dll.We apologize for the insufficient verification method.

As for the parent application, even if you press the close button, it won't end.
Minimize, stay in task tray, exit app from CotentMenuStrip menu
By selecting Exit, we create a flow to call Application.Exit() to exit.

Is this the cause?

Additionally, it seems that there are many causes overlapping, and the parent application uses FormClising
If you cancel the close, it doesn't seem to end during shutdown.
However, the name of the parent app does not appear on the warning screen in the example shutdown.For some reason,
Both parents and children are not closed, but only children are warned.

Also, by canceling the closure, the above SystemEvents_SessionEnding is also
It does not appear to run.

In addition, the problem is that the shutdown warning screen in the example is
SystemEvents_SessionEnding method. It appears before running, so even if you try to close it with this method,
It's too late (on Windows 10).

The meaning was different from the original title, but when the system shuts down,
I think I can solve this problem if I close the parent app properly.
Is there such a way?

I apologize for the inconvenience, but I appreciate your cooperation.

c#

2022-09-29 20:25

4 Answers

Timer calls Elapsed on a thread that is not the main thread.
Therefore, if you are simply viewing the external form in the Elapsed event handler, the form will become a form without a message loop and will not be able to respond to exit or other events (although I'm not confident that I'll look at the resulting behavior around here).

Currently, if you are not doing anything in the Elapsed event to display the form, you may want to create the form on the UI thread by referring to Marshaling Questions.


2022-09-29 20:25

If the SystemEvent.SessionEnding event may not occur in the UI thread, you can do the following:

private void SystemEvents_SessionEnding(object sender, SessionEndingEventArgse){
    if(Application.OpenForms.Count>0){
        varmainForm=Application.OpenForms[0];
        if(mainForm.InvokeRequired)
            mainForm.Invoke (new Action (Application.Exit);
        else
            Application.Exit();
    }
}


2022-09-29 20:25

Cancel property of FormClosingEventArgs should be false in the FormClosing event.


2022-09-29 20:25

Can't you judge by the FormClosingEventArgse.CloseReason value in FormClosing?
CloseReason.WindowsShutDown,CloseReason.TaskManagerClosingOther than that, minimize, reside in task tray, Cancel=True
(The only requirements mentioned in the question may be those other than CloseReason.WindowsShutDown.)


2022-09-29 20:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.