VB.NET Application.Exit not closing forms opened using ShowDialog - vb.net

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.

Related

Splash-screen threading error due to startup form closing

I have a splash-screen for a desktop app that I am making. The way the startup works is that it checks for certain files, if it finds them it closes immediately and loads up the main form.
However by doing so I get a threading error. I'm guessing its because the startup form closes before fully loading the Main form.
Here's the error message:
Cross-thread operation not valid: Control 'frm_SplashScreen' accessed from a thread other than the thread it was created on.
I've tried putting the thread to sleep, and even using timers to give the form time to load. But so far unsuccessfully.
Private Sub frm_Load_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If Dir("System.dll") = "" then
Createfile()
Frm_Main.show()
Me.close()
Else
Frm_Main.show()
Me.close()
End IF
Everything else regarding the splash-screen is done in the project properties under application. Otherwise it is just an empty form.
How can I fix this?
In WinForms, the splash screen is not responsible for creating the main form and should not do so. If you have code that needs to run before the main form is created, while the splash screen is visible, place that code in the application's Startup event. Here's how to do it:
Go to the application tab of your project's properties (same place you set the startup form and splash screen).
Click on "View Application Events"
In the event dropdown near the top of the window, select "Startup". This should create an event handler method for the Startup event, if it doesn't already exist (Private Sub MyApplication_Startup(sender, e) Handles Me.Startup).
Put your code (the If Dir(...) Then Createfile() stuff in your example code) in the event handler.
The code in the Startup event handler will be called before the main form is created. The splash screen will automatically be created, displayed, and destroyed in another thread. You don't need to worry about creating or closing the splash screen, as that is done automatically.
If your startup code determines that you don't want the main form to be displayed at all, set e.Cancel = true in the startup event handler method and the application will quit instead of bringing up the main form.
Well, according to my previous experience, this is a bug in visual studio, and it comes as a result of setting your startup form to window state.maximized that overrides the default launch event. So in order to fix this, you need the start up for to laugh at the startup then set the event on the splash screen's form closing event, instead of it running directly in the startup form.worked on over 5 projects. They all had the same issues till I moved the maximized state to the splash closing event e.g
Private sub splash_closed(....)
Startup form.window state.maximized...
End sub
So, the issue was actually event collision.

Application fail to exit when a runtime created object is used

I'm using vb.net 2013, and I have configured Shutdown mode to "When last form is closed".
On my main form, I have a menu item which has this code to close the application:
Application.Exit
Everything is working fine, except one case:
When I open a specific form, where a Combobox is created on runtime and I've used Addhandler to subscribe to several events.
The combobox is created when pressing a button.
When I open this form and I don't create the combobox, everything is working ok. If the combobox is created, when I close this form and try to close the application using the menu item, nothing happens. The application is not closed and no error message is displayed. (the same situation occurs when I try to close the main form with "x" button)
On the form's (where I have the combobox) close event , I tried to put a line of code:
MyCombobox.dispose()
But the situation is the same.
What can I do? Thank you!
What I do from my little experience is
1.) remove the MyCombobox from its parent control (I'm think this is in your combobox close event).
2.) set the MyCombobox to Nothing
3.) Dispose() it.
MyCombobox = Nothing
MyCombobox.Dispose()
It would be useful to see some part of your code for the close event so we can help you check. More power to you!
Update based on OP's comment:
I have read the following from MSDN: https://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing(v=vs.110).aspx
From this, it is important to note that:
The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.

Form_Disposed event Vs Form_Closing event in 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

VB.NET Invalid cross-thread access

I have a background worker control on a form.
In this form I have another form that shows a progress:
Private _fWait As frmWait
I am updating this form, change its label to tell the user what is currently going on.
When the background worker is finished, I want to close this form _fWait.
I am using
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
'do the background worker stuff. I have not stated it here because it is not important
_fWait.Close()
_bDone = True
End Sub
But I am getting the error "Invalid cross-thread access: The access to the control frmWait occured from a diffent thread than the thread it was created by." on the line "_fWait.Close()".
Can somebody tell me what I did wrong?
Thank you!
When you call _fWait.Close() is has to be called on the UI thread whereas the BackgroundWorker1_DoWork handler will be running on a different thread which is why you are getting the error. There are two options for closing the form:
1) Continue to close the form on the DoWork handler but marshal the request onto the UI thread using something like the code below:
this.Invoke(() => _fWait.Close());
It's been a while since I've done VB, so you'll have to convert the C# code into VB...sorry.
2) Handle the RunWorkerCompleted event and close it there. If you BackgroundWorker was started on
the UI thread, then the RunWorkerCompleted will also be called on the UI thread.
If I remember right the ProgressedChanged event will also be called on the UI assuming that the BackgroundWorker was created on the UI thread. If the BackgroundWorker is created another another thread, then the RunWorkerCompleted and ProgressChanged will also be called on a separate thread and you will have to marshal calls to the UI as described above in step 1.
In simple programs, you can ignore cross-thread errors by adding this to your Form.Load method:
CheckForIllegalCrossThreadCalls = False
Note that it can cause problems when running multiple asynchronous threads that access shared data, so don't use it indiscriminately.
If multiple threads cause a shared routine to run, use SyncLock to prevent multiple simultaneous instances.

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.