Form_Disposed event Vs Form_Closing event in VB.net - vb.net

I would like to know Form_Disposed event Vs Form_Closing event in VB.net.When I call application.exit function , the application is call form_closing event and form_disposed event in my computer.But I try to run this exe on remote server , the application does not work form_closing event.It it only run form_dispose event.I would like to know why application does not call form_closing event and different between closing and disposed event

Related

Can VBA code run after an application is closed?

I am using the ItemAdd event to watch for new Outlook emails. Does this event work while the application is closed?
Currently, my macro launches upon initial startup using the "Application_Startup()" event, and then initializes a class module containing a sub routine that is trigged with the "ItemAdd" event. If I close Outlook, will this macro still be watching for a new "ItemAdd" event?
Thanks!
No, it will not - the event is fired by the application only, so no application - no event.
Keep in mind that if this is a cached profile, these events will fire on application startup when the cached mailbox (OST) is updated. You can also work around this by processing all unread emails on startup (assuming they stay unread). Or you can persist the MailItem.ReceivedTime property of your last processed message and process all emails newer than that on startup.

Handle Application closing event without Dispose event handler

I'd like to know if there is a possible way to handle a closing application in Visual Studio 2008 without using the dispose event handler.
If my application crashes or if I close it while it is running:
Private Sub Foo_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
Is not called.
This result in a serious problem, because I'm currently working on multiple Excel files and they remain open after the application crashes or I close it while it's running.
Is there a way to handle this kind of closing application event?
In a normal situation when your application is simply closing you can subscribe to the MyApplication.Shutdown event and close your excel documents in there.
Subscribing to the event can be done through these steps:
Right-click your project in the Solution Explorer and press Properties.
Go to the Application pane and press View Application Events.
In the file that was opened, either write the event handler on your own or let VS do it by first selecting (MyApplication Events) in the left combo box above the text editor, then selecting Shutdown in the right combo box.
Now you should have an event handler that looks something like the one below. Just go ahead and do your cleanup in there:
Private Sub MyApplication_Shutdown(sender As Object, e As System.EventArgs) Handles Me.Shutdown
'Do your cleanup here...
End Sub
For application crashes caused by CLR exceptions you can use the AppDomain.UnhandledException event, but for more serious crashes there isn't very much you can do.
A workaround would be to create another application which monitors your main app. When the other app senses that your main application's process has been terminated, it will close the excel documents. The tricky part with this solution is passing the information necessary for the other app to close the documents.

VB.NET Application.Exit not closing forms opened using ShowDialog

I have a .Net application with multiple forms invoked using form.ShowDialog(). When I do Application.Exit, the application is not closing all the forms.
When I try to do an explicit Form.Close I am getting an error from WinForms as below
at Microsoft.AGL.Common.MISC.HandleAr(PAL_ERROR ar) at System.Windows.Forms.Form._CloseModal() at System.Windows.Forms.Form.Close() at
As #CharithJ states in this answer (paraphrased):
Cancel the background worker and send an argument to the BackgroundWorker RunWorkerCompleted event to identify that time has expired. From that event you can call Application.Exit() just fine.

"Suspend" execution, but keep event handlers running?

Windows 7 Home Premium
Visual Basic
Is there a way to suspend execution of a program, but keep event handlers running?
My Visual Basic console application watches for file events, and handles the events with the familiar event handlers.
Current algorithm:
1. Create FileSystemWatcher
2. Set up event handlers
3. Wait for console input from the user, then exit.
Can I replace the last step with a system call to suspend execution of the foreground, while the event handlers continue reacting to events?
Here's an outline of the code (with many lines removed).
Dim watcher As New FileSystemWatcher() 'Create a new FileSystemWatcher
AddHandler watcher.Changed, AddressOf OnChanged 'Add event handler
watcher.EnableRaisingEvents = True 'Begin watching
'I wish to replace this with a system call to suspend
'execution, but keep the event handlers running.
While Chr(Console.Read()) <> "q"c 'Wait for the user to quit
End While
It seems like you're trying to create a service. To do that, the easiest way is probably to create a new project based on the "Windows Service" template and migrate your existing code.
MSDN docs: http://msdn.microsoft.com/en-us/library/d56de412.aspx

how to close vb application when shutdown event occurs

I have created an application that starts when windows starts (on startup) but i want to unload my form or close my vb application securely without losing data
The FormClosing event should be the place where you handle expected or unexpected closing of your forms.
This event receives a FormClosingEventArgs that contains the reason for the shutdown of your form.
There is also a more generic Application.ApplicationExit but it lacks the details given by the FormClosingEventArg parameter passed to the FormClosing event.
How you handle the data that need to be saved could be the discriminant factor in choosing between the two methods.