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.
Related
We have a huge application that runs through a mass of code for every click you do. In one specific case, where you have a specific control in plain sight (so that the events are firing) and you load some special data into it and then click on the new button the form loses some events, like the closing event (you cannot close the form anymore and stop points in debug mode are not triggered) and one validating of a ComboBox that is really needed for the program. The only thing you can do when you get this kind of error is to restart the application to get out of this subform. But I tested it for 2 days now and even when I activate all points in the 'Exception Settings' I don't get any exceptions. The form (and other controls) are not losing ALL events. The resize, for example, still works. Even a specifically called AddHandler is not firing. Can you iterate somehow through the EventHandlerList? Or is there any way to test WHERE the event dies? Somehow any ideas on how to debug such a problem?
I have a usercontrol placed on a form at runtime.
I'm experiencing a UserControl_MouseLeave after FormClosing.
I'm wondering if this should happen under normal circumstances.
To test if this would occur normally as well (and not just in my huge project) I set up a test project with a tiny usercontrol (just having a certain backcolor), and I haven't been able to get the UserControl_MouseLeave event
after receiving the Form_Closing event.
This makes me wonder if my other usercontrol just takes really long to unload therefore doesn't go away right away and thus even stays alive after the FormClosing event.
Unfortunately I did not find any information about when the events can occur.
Does anybody know if this behaviour is normal?
Nothing is destroyed when the FormClosing event is fired so your observation about the disposing taking a long time might actually be correct.
When the form is closed, the WM_CLOSE method is called internally. Here's a flowchart of what happens:
Taken from MSDN article Closing the Window
In WM_CLOSE, the FormClosing event is raised in MDI children and all owned forms. Even after that, there's still a possibility to cancel the operation.
If we continue with the closing, the window is removed from the screen (but it still exists) Only after that, the WM_DESTROY message is sent to both the form itself and the child windows. MSDN states that:
During the processing of the message, it can be assumed that all child windows still exist.
If you have a very complex application that takes longer to dispose of, it's certainly possible that events are still fired after the FormClosing event and even if the form is not visible at that time.
It's also a possibility that there's something wrong with your code but without seeing it, this is what I think is the reason for the behaviour.
Edit: You can take a look at the insides of how the Form handles everything by checking out the Reference Source.
I have a win 7 application in vb.net for scanner data collection, the app first shows the main form -- top level, this form contains a lot of buttons, click any button will go to second level forms, based on scanned data input, the 3 level forms can show from a point of second level form(second level form not closed at this point), close the 3rd level form, you can go back to second level form at the point it is out for 3rd level form and continue to run rest program in send level form, close second lever form you can go back to main form to click another button to go. Main form just call the scanner class to initial, it will not handle the scanner event. the 2nd and 3rd level form load has the addhandler statement. No matter which form is on top will handle the scanner event to process scanned data in that particular form, every form process that data differently.
In order to keep the scanner event for every available form, I put the event in a module, and when the scanner class read a data will raise this event. when I test it, whatever the first available form handle the data well, and when the forms switch back and forth, the event handler not work any more, and program freeze or stop working. I cannot figure out why. I hope you guys can give me some advise about what is better way to handle this kind of event.
Thanks!
In a form, include a handler to call your function in the module. When the form is no longer active, unload the form or remove the scanner handler. If there are two active handlers for an event, it can cause problems.
I am writing an application in VB.Net in which a user can schedule emails to be sent in the future. Is there a way to keep my listener thread running in the background after the base form on the application is closed? I would also need to start up the listener when the system boots, but wouldn't want any forms to open at that time.
Example (desired) functionality: I open the application in the morning and schedule three emails, one of which should be sent in three hours, and the other two of which should be sent tomorrow morning. I close the application. In three hours, the first one sends. At the end of the day, I shut down my machine. When I turn it back on tomorrow morning, the other two are sent without me ever opening the application.
I am thinking I need to separate the listener into a service, but want to make sure there isn't an easier way before going down that road.
If I do end up having a separate service and application, can I create an installer that will install both at once?
In the options of your project, set the “Startup Object” to “Sub Main” instead of of a form name. You might have to disable the setting “Application Framework” first.
Now you have to launch the form manually because it’s no longer done automatically, but on the other hand you can now control when to launch it, and your application will exit when it reaches the end of the Main method (or when it’s quit explicitly) rather than when the form closes.
If all you want is to keep the program running, you could supply it with the definition for a TaskBar icon, and represent state in your application using the icon's graphic. It would appear over on the right next to the system clock.
Then it's just a matter of reconfiguring your development project so that the application close event is triggered off that icon's menu instead of off the fact that the form is closed.
Use a NotifyIcon control and when the user close the mainform capture the OnClosing Event to Ask the Question if he want to minimize to the tray or exit the application.
Here is the documentation of NotifyIcon control from Microsoft:
NotifyIcon Control
I have a possible solution for you here. In the form closing event you can place the following code:
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Me.Visible = False
e.Cancel = True
End Sub
This will hide the form and allow it to run in the background until you shutdown the computer. To handle the restart and then send emails I'd suggest adding the emails to be sent to a text file that can be accessed after restart and then adding the program to startup. An even better solution might be to have 2 programs, one that is on startup and is always hidden that checks the text file every 15 minutes (for example) and a second program that is launched when a new email is to be added to the list and it appends the text file with new emails to be sent.
(Sorry if i am wrong, just proposing a probable solution here)
I think attaching a simple db to hold the email sending schedule information/date-time and loading the application at startup with Notification Icon (or without Notification Icon but with a shortcut to show user interface/form to feed the emails and schedule information/date-time, if required + BackgroundWorker, if you want the application to go to sleep mode and wake it later to send first scheduled email; after X hours). After sending first email update the db then exit the application. Check the db on startup to send the first email or go to sleep mode if pc boots before sending first email. But, if first email is already sent then send other two scheduled emails (check db date-time). This should do the job...
:)
I have a form in a Pocket PC app that is set to be the Application form using:
Application.Run(New frmMain())
Somehow this form is getting closed for no reason while working on another form. No closing event is fired and nowhere in my code can you close the main form anyway. It is only closed by clicking the OK button at the top right of the window. When this is clicked, the user is prompted if he/she really wants to close the application. However, this is not occurring either.
Basically, I am on another form which adds a new record to the database. After adding the record the gotFocus event is somehow called for frmMain. When the code in the gotFocus event reaches a reference to a control on the form, there is an exception that states that the object is disposed.
When I bypass the code in the gotFocus event, the application just closes completely. I verify this by checking that it is not running in the device's memory.
I have been stepping through code for 2 hours and I have absolutely no idea why this could be occurring. Anyone possibly have a tip?
This sounds as an exception being raised anywhere. Maybe an exception on another thread? Look at the output window to see what it says.