How to delete file, then user closes program in WPF? - c#

I know full path of file var temp_file = Path.Combine(_directoryName1, "temp.ini"); which need to be deleted in the end of program working. How could I do this ? As I know it is possible to realize via OncloseEvent(). In addition, I dont know exatly how user will close application via alt+f4 or via buttons.
So far, I have tried to use this code below from almost the same question How to override onclose event on WPF?
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
//do my stuff before closing
base.OnClosing(e);
}
And I have added it in App.xaml.cs but it doesn't work. VS2013 says that he don't know such method base.OnClosing(e);
Is there any mistake or another way out?

Window.Closing is for a specific Window. That's probably not what you want in any case, because the Closing event can be cancelled. Window.Closed is likely a better choice.
To run something when the program is closed -- not tying yourself to a window -- you should subscribe to the Application.Exit event instead.

Related

TaskCanceledException after closing window

So first of all, I have multiple windows which I hide when opening another window, so I have to use
Application.Current.Shutdown();
to completely close the Application when pressing the "x" on the top right.
I wanted to handle the WindowClosing Event in my Home.xaml.cs file. But if I do this:
public Home()
{
InitializeComponent();
Closing += WindowClosing.OnWindowClosing;
}
then I am getting an System.Threading.Tasks.TaskCanceledException when I am closing the window.
Here is the WindowClosing event handler:
public static void OnWindowClosing(object sender, CancelEventArgs e)
{
Application.Current.Shutdown();
}
The weird part is, that I´ve done exactly the same with the login window, and it works there without any trouble.
I´ve stepped through it, and the closing event gets set as it should (in the Login.xaml.cs, as also in the Home.xaml.cs File).
I know this is not much Information for this error (I think thats all, but maybe the error is coming from somewhere else?!), but maybe someone else encountered this issue and can help me.
If you need more information just tell me, I will edit the question then.
Thanks for your help!
EDIT:
The Solution was taking Environment.Exit(0); instead of Application.Current.Shutdown();
In your OnWindowClosing() method, you can try substituting the following line for Application.Current.Shutdown():
Environment.Exit(0);

CM bootstraper showing a window before the root view

I have a Caliburn.Micro bootstrapper where I use OnStartup() to check a view things and call DisplayRootViewFor<IShell> later. In some scenarios I must have the user to make an important decision or show some warning before the root view launches. I can do this by using WindowManager.ShowDialog().
But here is the problem: When I have no window shown before the root view, all works like expected.
When I show a window before the root view the DisplayRootViewFor() call is made but the application terminate immediately.
I guess this is because when I use the window manager to show a window before it gets the root view and closing it makes the WPF application thinking its main window is closed and it terminates automatically.
So how can I show a (modal) window before the root view?
I found one possible solution is:
Set Application.ShutdownMode to OnExplicitShutdown. Then I have to track when really shutting down the application like when the shell is closed I have to call System.Windows.Application.Current.Shutdown(); explicitly.
I am curious if there is also another way but I guess when the modal window is closed the for a moment the number of windows in the WPF application goes down to 0 the shutdown is triggered regardless if a new main window is established shortly after.
you can put your shutdown on your shell view on close as the shell view is the main window if the user close it means they want to close the exe.
private void ShellView_OnClosed(object sender, EventArgs e)
{
Application.Current.Shutdown();
}
Well, if someone else is having this problem, I solved it overriding the OnActivate method like this:
protected override void OnActivate()
{
_windowManager.ShowDialog(YOUR_WINDOW);
base.OnActivate();
}
Thus you don't have to set Application.ShutdownMode to OnExplicitShutdown and your application works normally.

Eventhandler does not fire

I have a class which performs an async operation.
While that async operation (which works downloads a file) it should show a new dialog.
To close that dialog again and also show a progress in it, I made events that are fired and should let the event handlers do actions.
To show you that a bit clearer:
var dialog = new DownloadDialog();
DoAsyncDownload();
if (dialog.ShowDialog() == DialogResult.OK)
{
dialog.Close();
// Go on
}
So the download dialog is just there to show the user that a download is going on.
In order to show a progress and close the dialog then again, I want to work event-based.
What I made is an event:
public event EventHandler<EventArgs> DownloadFinished();
protected virtual void OnDownloadFinished(Object sender, EventArgs e)
{
if (DownloadFinished != null) DownloadFinished(sender, e);
}
Well, so in the method that is called async I call it like that, when the file is downloaded:
OnDownloadFinished();
Then this event is fired.
So, in order to make the dialog receieve this event I set an handler with the correct signature in the dialog's class.
Then I set this handler like that:
this.DownloadFinished += new EventHandler<EventArgs>(dialog.DownloadFinishedHandler);
But the event handler does never execute its code. :(
I checked this with breakpoints.
I think the problem is using a webclient to download the file. Its eventhandlers are not set at the right time I think.
The webclient is declared as a member outside any functions or anything, so that every function can access it.
Then the code in the method that is called async looks like that:
packageDownloader.DownloadFile(MyUrl, "Url");
OnPackageDownloadFinished(this, EventArgs.Empty);
Like I already said above, this functions is called then.
The same problem is appearing with the progress changed, it is the same.
The thing I am sure that there is any way this would work or the webclient is the guilty guy, is that I have already done the same thing with another method and there the eventhandlers are called and everything works fine. So in this other method another thing is done, but after that it also raises an event. And the handler set is exactly the same.
Only here it doesn't work, I think it is the webclient.
PS: If you have any questions or something isn't clear to you, ask me. ;)
The problem was very dumb. I didn't download the file asnyc, so no events could be raised that clear way.

Invoking several methods after ordering the program closed

I've got a WinForm Application that connects via Net.Sockets to another program.
When it gets closed per X or ALT+F4 or whatever, several routines should be processed and I need to keep the network connection until they're done.
I thought an Application.ApplicationExit Handler would do the trick, but as soon as I give the order the network connection is dead. Other important stuff can't be done.
My Form is closed instantaneously too, and I need that one a little longer.
Is Application.ApplicationExit the right tool for the job?
I put that in my Form Class that App.Runs from the Main method which is located in a different class.
You could use the Closing event of the form. It is called before the form is closed.
If you are having an open window, there are the event-handler Closing and Closed. The Closing-event is fired as soon as you click 'X', for example. Inside the method you can even decide, whether to close the window or not.
Provide an event-handler like
private void Form1_Closing(object sender, FormClosingEventArgs e){
// persist until everything is done
}
and associate it with the event
Form1.Closing += Form1_Closing;
See MSDN for documentation.

Why won't Application.Exit() quit a Windows Forms application?

I am working on learning Windows Forms with C# and have a bare bones application. I am trying to close it when the user selects File->Exit. I have an event handler attached to it and I have tried calling Application.Exit(), Application.ExitThread() and just closing the form. Nothing. It stays there. I'm not creating any other threads of any sort either.
Ideas? Thanks.
Have you tried to put a breakpoint in the event handler to see if it is being hit?
If so, the application won't exit if the window messages aren't being delivered (i.e. the UI thread is blocked). One way to test this is to call Environment.Exit() which is more brutal about forcing a close. If this succeeds, you can then figure out why Application.Exit() isn't working.
Application.Exit isn't the normal way to close a GUI application. Use form.Close instead.
private static void OnMenuClose_Click(object sender, System.EventArgs e)
{
Form dlg = ((Control) sender).FindForm();
//dlg.DialogResult = DialogResult.OK;
dlg.Close();
}

Categories