How to create forms in background worker thread in vb.net
-
Hey i am trying to dynamically create a form in background worker but the after the run the application the form appears as 'not responding? Please advise Can we create a form in background worker thread and update the controls in the same thread? Here is how i am doing it... Private Sub DoJob(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Dim f As New System.Windows.Forms.Form Dim label1 As New System.Windows.Forms.TextBox f.Size = New System.Drawing.Size(487, 416) f.Name = "Form6" f.Text = "Form6" f.Location = New System.Drawing.Point(0, 0) label1.Name = "text1" label1.Size = New System.Drawing.Size(100, 20) label1.Location = New System.Drawing.Point(54, 37) f.BackColor = Color.Gray f.Activate() f.Controls.Add(label1) f.Show() End Sub After I run it can see form6 ,but in not i responsive state.What am I doing wrong here?
-
Hey i am trying to dynamically create a form in background worker but the after the run the application the form appears as 'not responding? Please advise Can we create a form in background worker thread and update the controls in the same thread? Here is how i am doing it... Private Sub DoJob(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Dim f As New System.Windows.Forms.Form Dim label1 As New System.Windows.Forms.TextBox f.Size = New System.Drawing.Size(487, 416) f.Name = "Form6" f.Text = "Form6" f.Location = New System.Drawing.Point(0, 0) label1.Name = "text1" label1.Size = New System.Drawing.Size(100, 20) label1.Location = New System.Drawing.Point(54, 37) f.BackColor = Color.Gray f.Activate() f.Controls.Add(label1) f.Show() End Sub After I run it can see form6 ,but in not i responsive state.What am I doing wrong here?
Hi, This is a threading issue. Try to move your code from DoWork to a new sub e.g. 1. Private Sub CreateNewForm() Dim f As New System.Windows.Forms.Form Dim label1 As New System.Windows.Forms.TextBox f.Size = New System.Drawing.Size(487, 416) f.Name = "Form6" f.Text = "Form6" f.Location = New System.Drawing.Point(0, 0) label1.Name = "text1" label1.Size = New System.Drawing.Size(100, 20) label1.Location = New System.Drawing.Point(54, 37) f.BackColor = Color.Gray f.Activate() f.Controls.Add(label1) f.Show() End Sub 2. At the member level add: Private Delegate Sub delCreateNewForm() 3. In the DoWork sub invoke the CreateNewForm sub: Private Sub DoJob(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Dim d As New delCreateNewForm(AddressOf CreateNewForm) Me.Invoke(d) End Sub Hope this helps :)
NajiCo http://www.InsideVB.NET[^] It's nice 2b important, but it's more important 2b nice... http://www.facebook.com/group.php?gid=5932660937[^]
-
Hi, This is a threading issue. Try to move your code from DoWork to a new sub e.g. 1. Private Sub CreateNewForm() Dim f As New System.Windows.Forms.Form Dim label1 As New System.Windows.Forms.TextBox f.Size = New System.Drawing.Size(487, 416) f.Name = "Form6" f.Text = "Form6" f.Location = New System.Drawing.Point(0, 0) label1.Name = "text1" label1.Size = New System.Drawing.Size(100, 20) label1.Location = New System.Drawing.Point(54, 37) f.BackColor = Color.Gray f.Activate() f.Controls.Add(label1) f.Show() End Sub 2. At the member level add: Private Delegate Sub delCreateNewForm() 3. In the DoWork sub invoke the CreateNewForm sub: Private Sub DoJob(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Dim d As New delCreateNewForm(AddressOf CreateNewForm) Me.Invoke(d) End Sub Hope this helps :)
NajiCo http://www.InsideVB.NET[^] It's nice 2b important, but it's more important 2b nice... http://www.facebook.com/group.php?gid=5932660937[^]
hey thanks for the help it is working.:-) So now i will be generating many of the same form this way by using a list of background workers.So will each form be running in its own thread? Also how can i update the form's gui from the DoWork sub?
-
hey thanks for the help it is working.:-) So now i will be generating many of the same form this way by using a list of background workers.So will each form be running in its own thread? Also how can i update the form's gui from the DoWork sub?
Sohaib_A wrote:
So now i will be generating many of the same form this way by using a list of background workers.So will each form be running in its own thread?
No, they won't. Perhaps you didn't get the implied answer: You can't create a window in a background thread, you have to use Invoke to create the window in the GUI thread. (It might be possible to create a window from a thread, but then the thread would have to run a message pump just as the GUI thread does. That would be pointless as you already have a message pump.)
Sohaib_A wrote:
Also how can i update the form's gui from the DoWork sub?
The same way; you use Invoke to run the code in the GUI thread. Alternatively, you put updates in a synchronised queue, and have a timer in the GUI thread read updates from the queue and display them.
Despite everything, the person most likely to be fooling you next is yourself.