Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to create forms in background worker thread in vb.net

How to create forms in background worker thread in vb.net

Scheduled Pinned Locked Moved Visual Basic
csharpmobilegraphicstutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sohaib_a
    wrote on last edited by
    #1

    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?

    N 1 Reply Last reply
    0
    • S sohaib_a

      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?

      N Offline
      N Offline
      Naji El Kotob
      wrote on last edited by
      #2

      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[^]

      S 1 Reply Last reply
      0
      • N Naji El Kotob

        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[^]

        S Offline
        S Offline
        sohaib_a
        wrote on last edited by
        #3

        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?

        G 1 Reply Last reply
        0
        • S sohaib_a

          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?

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups