473,471 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Form is disposed when the thread completes

rizwan6feb
108 New Member
I know the problem but can't find the solution. Please help

I have 2 forms in my application (Form1 and Form2, Form1 is the startup form). I am connecting to the database in a new thread (In the FormLoad event of Form1, please see the code below) . If the connection to database on line# 4 fails i show Form2 (Which is basically a DB settings Form) but the form is disposed when the thread completes . How can make Form2 stay on the screen. Please see the code below

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private Sub connectDB()
  3.         Try
  4.             'DB connection code goes here ...
  5.  
  6.         Catch ex As Exception
  7.             Dim f As New Form2
  8.             f.Show()
  9.         End Try
  10.     End Sub
  11.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  12.         Dim t As New Threading.Thread(AddressOf connectDB)
  13.         t.IsBackground = False
  14.         t.Start()
  15.     End Sub
  16. End Class
  17.  
Sep 1 '08 #1
3 1227
joedeene
583 Contributor
I know the problem but can't find the solution. Please help

I have 2 forms in my application (Form1 and Form2, Form1 is the startup form). I am connecting to the database in a new thread (In the FormLoad event of Form1, please see the code below) . If the connection to database on line# 4 fails i show Form2 (Which is basically a DB settings Form) but the form is disposed when the thread completes . How can make Form2 stay on the screen. Please see the code below

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private Sub connectDB()
  3.         Try
  4.             'DB connection code goes here ...
  5.  
  6.         Catch ex As Exception
  7.             Dim f As New Form2
  8.             f.Show()
  9.         End Try
  10.     End Sub
  11.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  12.         Dim t As New Threading.Thread(AddressOf connectDB)
  13.         t.IsBackground = False
  14.         t.Start()
  15.     End Sub
  16. End Class
  17.  
Expand|Select|Wrap|Line Numbers
  1.    Private Sub connectDB()
  2.         Try
  3.             'DB connection code goes here ...
  4.  
  5.         Catch ex As Exception
  6.             Dim f As New Form2
  7.             f.Show()
  8.             Do While Form2.myvariabletoclose = False
  9.                 Threading.Thread.Sleep(750)
  10.             Loop ' and in the closing or OK button click of form2 set the variable to close to TRUE, but you might have to make this thread background so it doesnt freeze        End Try
  11.     End Sub
but im pretty sure it will freeze unless you set the background property to true, and im not sure if you can access pre-dimmed variables on the main thread, from a child thread...well try it and let me know...

joedeene
Sep 1 '08 #2
vekipeki
229 Recognized Expert New Member
The problem is that you are creating your Form2 instance in a different thread, so it is natural that it will be closed as soon as the thread has ended.

Whenever you use multiple threads in .Net be careful to invoke all Gui related commands from a Gui thread. Search help for InvokeRequired property, and BeginInvoke() method. You should end up with something like this:

Expand|Select|Wrap|Line Numbers
  1. Private Delegate Sub DelegateShowFormOnMainThread()
  2. Private Sub ShowFormOnMainThread()
  3. ' Check if this method was called from a different thread
  4. If Me.InvokeRequired() Then
  5.    ' If yes, then invoke this method again from a Gui thread
  6.    Dim d As DelegateShowFormOnMainThread = New DelegateShowFormOnMainThread(AddressOf ShowFormOnMainThread)
  7.    Me.BeginInvoke(d) ' Me is the instance of Form1, so this will invoke this same method as soon as the main Gui thread is ready
  8. Else
  9.    ' If invoke is not required (meaning that this method was called with BeginInvoke()), do the actual work
  10.    Dim form2 As New Form2
  11.    form2.Show()
  12. End If
  13. End Sub
Call the ShowFormOnMainThread() method from your background thread to invoke it on a main thread.

This is a very common issue when working with .NET Gui from different threads, so you should get used to it. :)
Sep 1 '08 #3
rizwan6feb
108 New Member
Thanks vekipeki for your help. It took me a day to understand this little function but this right way to deal with multiple threads
Sep 6 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Jim Hammond | last post by:
The following code tries to excute a function 10 seconds after Page_Load by using a Timer, but the callback never gets called. private void Page_Load(object sender, System.EventArgs e) { ...
6
by: bole2cant | last post by:
The error I get is: Cannot access a disposed Object named "Form2". Object name "Form2". Here is the sequence which gives the error. Start program. Form1 comes up and has a button to choose...
3
by: PAPutzback | last post by:
I have a form that when it loads it fires off a second thread to load a datatable. This datatable contains a list of names and ids that the user may want to add to their criteria. So I have a...
8
by: PAPutzback | last post by:
How do I keep the form up.
0
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
I read from a serialport using a worker thread. Because the worker thread t does not loop often, I cannot wait to terminate the worker thread using a boolean in the While condition. So I have a...
2
by: Mike | last post by:
Hello, Ok I have 2 classes in my project, one is the main form and one is a connection class, at a certain event on my main form a new instance is made of the connection class, and a reference...
22
by: Zytan | last post by:
I have public methods in a form. The main form calls them, to update that form's display. This form is like a real-time view of data that is changing. But, the form may not exist (it is...
5
by: aine_canby | last post by:
Hi, The problem is that the line - this.Invoke(new MyDelegate(Function), args); // this = MainForm is being called after the line - terminatePopulate = true; therefore I get an exception...
2
by: BillE | last post by:
Using vb.net 2008 windows forms. How can a form which has been closed and disposed still attempt to handle a custom event? I have a form which inherits from a base form class. The base...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.