Opening forms in background thread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    Opening forms in background thread

    Hi

    I have a number of forms which open like this;

    Dim frm1 As frmForm1 = New frmForm1
    Dim frm2 As frmForm2 = New frmForm2
    Dim frm3 As frmForm3 = New frmForm3
    Dim frm4 As frmForm4 = New frmForm4

    .....

    and so on and then finally open the first form with frm1.Show().


    My question is, after opening first form using Dim frm1 As frmForm1 = New
    frmForm1, how can I open the rest of the forms in a background thread? Would
    appreciate a code example.

    Many Thanks

    Regards


  • Tom Shelton

    #2
    Re: Opening forms in background thread

    On Apr 24, 3:48 pm, "John" <i...@nospam.in fovis.co.ukwrot e:
    Hi
    >
    I have a number of forms which open like this;
    >
    Dim frm1 As frmForm1 = New frmForm1
    Dim frm2 As frmForm2 = New frmForm2
    Dim frm3 As frmForm3 = New frmForm3
    Dim frm4 As frmForm4 = New frmForm4
    >
    ....
    >
    and so on and then finally open the first form with frm1.Show().
    >
    My question is, after opening first form using Dim frm1 As frmForm1 = New
    frmForm1, how can I open the rest of the forms in a background thread? Would
    appreciate a code example.
    >
    Many Thanks
    >
    Regards
    Ok... you'll want to just add these classes to a project and copy them
    in one at a time:

    Option Strict On
    Option Explicit On
    Option Infer Off

    Imports System.Threadin g

    Public Class MainForm
    Inherits System.Windows. Forms.Form

    Public Sub New()
    InitializeCompo nent()
    Button1.Tag = GetType(Form1)
    Button2.Tag = GetType(Form2)
    Button3.Tag = GetType(Form3)

    End Sub

    #Region "Designer Code"
    'Form overrides dispose to clean up the component list.
    <System.Diagnos tics.DebuggerNo nUserCode()_
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    Try
    If disposing AndAlso components IsNot Nothing Then
    components.Disp ose()
    End If
    Finally
    MyBase.Dispose( disposing)
    End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.Componen tModel.IContain er

    'NOTE: The following procedure is required by the Windows Form
    Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    <System.Diagnos tics.DebuggerSt epThrough()_
    Private Sub InitializeCompo nent()
    Me.Button1 = New System.Windows. Forms.Button
    Me.Button2 = New System.Windows. Forms.Button
    Me.Button3 = New System.Windows. Forms.Button
    Me.SuspendLayou t()
    '
    'Button1
    '
    Me.Button1.Loca tion = New System.Drawing. Point(12, 12)
    Me.Button1.Name = "Button1"
    Me.Button1.Size = New System.Drawing. Size(268, 23)
    Me.Button1.TabI ndex = 0
    Me.Button1.Text = "Button1"
    Me.Button1.UseV isualStyleBackC olor = True
    '
    'Button2
    '
    Me.Button2.Loca tion = New System.Drawing. Point(12, 41)
    Me.Button2.Name = "Button2"
    Me.Button2.Size = New System.Drawing. Size(268, 23)
    Me.Button2.TabI ndex = 1
    Me.Button2.Text = "Button2"
    Me.Button2.UseV isualStyleBackC olor = True
    '
    'Button3
    '
    Me.Button3.Loca tion = New System.Drawing. Point(12, 70)
    Me.Button3.Name = "Button3"
    Me.Button3.Size = New System.Drawing. Size(268, 23)
    Me.Button3.TabI ndex = 2
    Me.Button3.Text = "Button3"
    Me.Button3.UseV isualStyleBackC olor = True
    '
    'Form1
    '
    Me.AutoScaleDim ensions = New System.Drawing. SizeF(6.0!, 13.0!)
    Me.AutoScaleMod e = System.Windows. Forms.AutoScale Mode.Font
    Me.ClientSize = New System.Drawing. Size(292, 113)
    Me.Controls.Add (Me.Button3)
    Me.Controls.Add (Me.Button2)
    Me.Controls.Add (Me.Button1)
    Me.Name = "Form1"
    Me.Text = "Form1"
    Me.ResumeLayout (False)

    End Sub
    Friend WithEvents Button1 As System.Windows. Forms.Button
    Friend WithEvents Button2 As System.Windows. Forms.Button
    Friend WithEvents Button3 As System.Windows. Forms.Button
    #End Region

    Private Sub ClickHandler(By Val sender As Object, ByVal e As
    EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
    Dim t As New Thread(AddressO f ShowForm)
    t.Start(DirectC ast(sender, Control).Tag)
    End Sub

    Public Sub ShowForm(ByVal ft As Object)
    Dim t As Type = TryCast(ft, Type)
    If Not t Is Nothing Then
    Using f As Form = TryCast(Activat or.CreateInstan ce(t,
    False), Form)
    If Not f Is Nothing Then
    Application.Run (f)
    End If
    End Using
    End If
    End Sub
    End Class

    Public Class Form1
    Inherits System.Windows. Forms.Form

    Public Sub New()
    InitializeCompo nent()
    End Sub

    Private Sub InitializeCompo nent()
    Me.SuspendLayou t()
    '
    'Form1
    '
    Me.ClientSize = New System.Drawing. Size(292, 266)
    Me.Name = "Form1"
    Me.Text = "Hi! Form1 here"
    Me.ResumeLayout (False)

    End Sub
    End Class

    Public Class Form2
    Inherits System.Windows. Forms.Form

    Public Sub New()
    InitializeCompo nent()
    End Sub

    Private Sub InitializeCompo nent()
    Me.SuspendLayou t()
    '
    'Form1
    '
    Me.ClientSize = New System.Drawing. Size(292, 266)
    Me.Name = "Form1"
    Me.Text = "Hi! Form2 here"
    Me.ResumeLayout (False)

    End Sub
    End Class

    Public Class Form3
    Inherits System.Windows. Forms.Form

    Public Sub New()
    InitializeCompo nent()
    End Sub

    Private Sub InitializeCompo nent()
    Me.SuspendLayou t()
    '
    'Form1
    '
    Me.ClientSize = New System.Drawing. Size(292, 266)
    Me.Name = "Form1"
    Me.Text = "Hi! Form3 here"
    Me.ResumeLayout (False)

    End Sub
    End Class

    HTH

    --
    Tom Shelton

    Comment

    • John

      #3
      Re: Opening forms in background thread

      Hi Tom

      Many Thanks

      Regards

      "Tom Shelton" <tom_shelton@co mcast.netwrote in message
      news:013c1fea-93b5-4ac4-9c8c-d9b51287c900@m3 6g2000hse.googl egroups.com...
      On Apr 24, 3:48 pm, "John" <i...@nospam.in fovis.co.ukwrot e:
      Hi
      >
      I have a number of forms which open like this;
      >
      Dim frm1 As frmForm1 = New frmForm1
      Dim frm2 As frmForm2 = New frmForm2
      Dim frm3 As frmForm3 = New frmForm3
      Dim frm4 As frmForm4 = New frmForm4
      >
      ....
      >
      and so on and then finally open the first form with frm1.Show().
      >
      My question is, after opening first form using Dim frm1 As frmForm1 = New
      frmForm1, how can I open the rest of the forms in a background thread?
      Would
      appreciate a code example.
      >
      Many Thanks
      >
      Regards
      Ok... you'll want to just add these classes to a project and copy them
      in one at a time:

      Option Strict On
      Option Explicit On
      Option Infer Off

      Imports System.Threadin g

      Public Class MainForm
      Inherits System.Windows. Forms.Form

      Public Sub New()
      InitializeCompo nent()
      Button1.Tag = GetType(Form1)
      Button2.Tag = GetType(Form2)
      Button3.Tag = GetType(Form3)

      End Sub

      #Region "Designer Code"
      'Form overrides dispose to clean up the component list.
      <System.Diagnos tics.DebuggerNo nUserCode()_
      Protected Overrides Sub Dispose(ByVal disposing As Boolean)
      Try
      If disposing AndAlso components IsNot Nothing Then
      components.Disp ose()
      End If
      Finally
      MyBase.Dispose( disposing)
      End Try
      End Sub

      'Required by the Windows Form Designer
      Private components As System.Componen tModel.IContain er

      'NOTE: The following procedure is required by the Windows Form
      Designer
      'It can be modified using the Windows Form Designer.
      'Do not modify it using the code editor.
      <System.Diagnos tics.DebuggerSt epThrough()_
      Private Sub InitializeCompo nent()
      Me.Button1 = New System.Windows. Forms.Button
      Me.Button2 = New System.Windows. Forms.Button
      Me.Button3 = New System.Windows. Forms.Button
      Me.SuspendLayou t()
      '
      'Button1
      '
      Me.Button1.Loca tion = New System.Drawing. Point(12, 12)
      Me.Button1.Name = "Button1"
      Me.Button1.Size = New System.Drawing. Size(268, 23)
      Me.Button1.TabI ndex = 0
      Me.Button1.Text = "Button1"
      Me.Button1.UseV isualStyleBackC olor = True
      '
      'Button2
      '
      Me.Button2.Loca tion = New System.Drawing. Point(12, 41)
      Me.Button2.Name = "Button2"
      Me.Button2.Size = New System.Drawing. Size(268, 23)
      Me.Button2.TabI ndex = 1
      Me.Button2.Text = "Button2"
      Me.Button2.UseV isualStyleBackC olor = True
      '
      'Button3
      '
      Me.Button3.Loca tion = New System.Drawing. Point(12, 70)
      Me.Button3.Name = "Button3"
      Me.Button3.Size = New System.Drawing. Size(268, 23)
      Me.Button3.TabI ndex = 2
      Me.Button3.Text = "Button3"
      Me.Button3.UseV isualStyleBackC olor = True
      '
      'Form1
      '
      Me.AutoScaleDim ensions = New System.Drawing. SizeF(6.0!, 13.0!)
      Me.AutoScaleMod e = System.Windows. Forms.AutoScale Mode.Font
      Me.ClientSize = New System.Drawing. Size(292, 113)
      Me.Controls.Add (Me.Button3)
      Me.Controls.Add (Me.Button2)
      Me.Controls.Add (Me.Button1)
      Me.Name = "Form1"
      Me.Text = "Form1"
      Me.ResumeLayout (False)

      End Sub
      Friend WithEvents Button1 As System.Windows. Forms.Button
      Friend WithEvents Button2 As System.Windows. Forms.Button
      Friend WithEvents Button3 As System.Windows. Forms.Button
      #End Region

      Private Sub ClickHandler(By Val sender As Object, ByVal e As
      EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
      Dim t As New Thread(AddressO f ShowForm)
      t.Start(DirectC ast(sender, Control).Tag)
      End Sub

      Public Sub ShowForm(ByVal ft As Object)
      Dim t As Type = TryCast(ft, Type)
      If Not t Is Nothing Then
      Using f As Form = TryCast(Activat or.CreateInstan ce(t,
      False), Form)
      If Not f Is Nothing Then
      Application.Run (f)
      End If
      End Using
      End If
      End Sub
      End Class

      Public Class Form1
      Inherits System.Windows. Forms.Form

      Public Sub New()
      InitializeCompo nent()
      End Sub

      Private Sub InitializeCompo nent()
      Me.SuspendLayou t()
      '
      'Form1
      '
      Me.ClientSize = New System.Drawing. Size(292, 266)
      Me.Name = "Form1"
      Me.Text = "Hi! Form1 here"
      Me.ResumeLayout (False)

      End Sub
      End Class

      Public Class Form2
      Inherits System.Windows. Forms.Form

      Public Sub New()
      InitializeCompo nent()
      End Sub

      Private Sub InitializeCompo nent()
      Me.SuspendLayou t()
      '
      'Form1
      '
      Me.ClientSize = New System.Drawing. Size(292, 266)
      Me.Name = "Form1"
      Me.Text = "Hi! Form2 here"
      Me.ResumeLayout (False)

      End Sub
      End Class

      Public Class Form3
      Inherits System.Windows. Forms.Form

      Public Sub New()
      InitializeCompo nent()
      End Sub

      Private Sub InitializeCompo nent()
      Me.SuspendLayou t()
      '
      'Form1
      '
      Me.ClientSize = New System.Drawing. Size(292, 266)
      Me.Name = "Form1"
      Me.Text = "Hi! Form3 here"
      Me.ResumeLayout (False)

      End Sub
      End Class

      HTH

      --
      Tom Shelton


      Comment

      Working...