i want to delete all items from my custom made folder, but i want to do it when the application closes as the user no longer needs a handle on these files anymore.
Where in the c# code should i write such a method? For example is there a application.shutdwon event or something :$
Thanks
Winforms has an ApplicationExit event.
WPF has an Exit event.
You can write your cleanup code in the event handler.
Related
I've been struggling with this for quite a while
My wpf application contains a list view, populated with file-names, which are located on a server.
I'm trying to implement drag and drop functionality, so the user can drag files from my application into his/her's local computer.
In order to do this, first i'm downloading the files into a temporary location, and then calling my application's DoDragDrop() method.
The problem is that I want to perform the download process only after the DoDragDrop method is called.
I've tried every event related to drag drop methods (GiveFeedback, ItemDrag, etc...) but nothing works
so basically what I need is an event, raised after the DoDragDrop is done
any ideas?
The current scheme is to drag a file with a special suffix, and then turn on global file monitoring to obtain the drag location of the file through monitoring.
I`m currently trying to handle cases in my app for exiting, such as Application.Exit (Both via .xml and in .xaml.cs), Dispatcher Exception, etc.
However, I can't seem to find any way to simply listen to a System.exit() command with an error. An example of this is when the app is running and it is stopped in Visual Studio (Shift+F5), where it logs that a System.exit(-1) happened.
Is there a way to listen to this in the Application context?
You can subscribe to the closing and/or closed event in your view.
If you subscribe to the events in your main view this may do the job for you.
I have written a C# utility class (DLL), which gets called by a windows application. Within the windows application, I have setup a backgrounderWorker to allow for time-consuming code not to hog the windows application. The time-consuming code is in the utility class DLL. Now, I setup two buttons on the windows application a 'submit' button - where the time-consuming code gets called in the utility class and a 'cancel' button.
I would like the 'cancel' button to stop the backgroundWorker code if clicked. Problem is the the 'Cancel' button is in the windows application and the code is in the DLL. So is there a way for me to maybe attach the 'cancel' button's 'onClick' eventHandler to the DLL and then have the DLL periodically check to see if the button was pressed?
BTW, the cancel button does work up until the DLL code gets initiated.
Am I correct in my thoughts or is there a better way? Any help would be appreciated.
Yes, It is possible to register to events between projects. all you have to do is have a reference of the winforms application inside your dll, and have the cancel button public. then you can register to any of it's events inside the dll code. However, I'm not sure that this is the best way to do what you want. I think a better way to do it is have a method called Cancel inside the dll and have the cancel button click event call this function. This way your dll is less dependent on your winforms application.
I actually went back and looked at some documentation I had about events and event handling. I did the following in the DLL:
Made a public class an added an eventHandler, my own custom eventArgs, and a subclass (eventWatcher) to host an event (call it onCancel)
I registered eventWatcher to listen for the OnCancel event in the DLL.
I made the eventWatcher public so it can be consumed in the client app.
In the client app, I did the following:
I modified the cancel button to raise the onCancel event. This allowed me to assign a method in the DLL to handle the graceful exiting of the time-consuming code in the DLL.
It now works as expected!
I have searched through stackoverflow and has found similar question.
As I am still a beginner in C# programming, I could not quite understand what the solutions meant.
I have a c# windows form application that has several forms.
As it only closes when I closes the main form, how do I code it such that when I close other form the application closes as well?
EDIT: most people has told me to code it in the closing event. may i know where is the event found at? thanks.
Not sure why you think out answers will be any easier than ones you have found already but..
A Form has an event called "Closing" (See Here). You should handle the event and then inside that function you can use the following line to close your application...
System.Windows.Forms.Application.Exit();
NOTE: This is for a WinForms application
EDIT:
You can register the closing event using VS designer mode like this...
Select the form
Go To the "Events" window
Double click the "Closing" event option
More detailed answer here
(Google if you cannot work this out)
or you can register the event in code like this...
//In Form constructor function
this.Closing += new EventHandler(Form1_Closing);
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//Handle event here
System.Windows.Forms.Application.Exit();
}
In WPF you can call Application.Current.Shutdown to close the application.
Register in your window to the Window.Closed-Event and call Application.Current.Shutdown()
Update
For Winforms, use the FormClosedEvent or override OnFormClosed. As alreads written by others, use Application.Exit to terminate the app.
WPF stands for Windows Presentation Foundation and is a framework for building nice user interfaces
Its very simple. Use the following code if you want to exit from the application from any point of time.
Application.Exit();
Check out Application.Exit Method
Informs all message pumps that they must terminate, and then closes
all application windows after the messages have been processed.
There is also an overload that allows forms receiving the quit event to inform it wants to cancel.
I have a c# WPF application that needs to save data when it closes. The Window Closing/Closed events work fine if the user closes the program, but they do not get called if the user logs off/shutdown the computer.
I have found ways to catch this event in winforms programs (here, and here). but i cant figure out how to achieve this in a WPF application.
I'm trying to halt the shutdown until my program is ready to exit
There is a built-in event Application.SessionEnding - this event fires when the user logs off or shuts down the computer... you just need to subscribe to that and put your code to save date etc. in there...