Opening forms in background thread

  • Thread starter Thread starter John
  • Start date Start date
J

John

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
 
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.Threading

Public Class MainForm
Inherits System.Windows.Forms.Form

Public Sub New()
InitializeComponent()
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.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'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.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(12, 12)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(268, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
Me.Button1.UseVisualStyleBackColor = True
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(12, 41)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(268, 23)
Me.Button2.TabIndex = 1
Me.Button2.Text = "Button2"
Me.Button2.UseVisualStyleBackColor = True
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(12, 70)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(268, 23)
Me.Button3.TabIndex = 2
Me.Button3.Text = "Button3"
Me.Button3.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.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(ByVal sender As Object, ByVal e As
EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
Dim t As New Thread(AddressOf ShowForm)
t.Start(DirectCast(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(Activator.CreateInstance(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()
InitializeComponent()
End Sub

Private Sub InitializeComponent()
Me.SuspendLayout()
'
'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()
InitializeComponent()
End Sub

Private Sub InitializeComponent()
Me.SuspendLayout()
'
'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()
InitializeComponent()
End Sub

Private Sub InitializeComponent()
Me.SuspendLayout()
'
'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
 
Hi Tom

Many Thanks

Regards

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.Threading

Public Class MainForm
Inherits System.Windows.Forms.Form

Public Sub New()
InitializeComponent()
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.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'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.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(12, 12)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(268, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
Me.Button1.UseVisualStyleBackColor = True
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(12, 41)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(268, 23)
Me.Button2.TabIndex = 1
Me.Button2.Text = "Button2"
Me.Button2.UseVisualStyleBackColor = True
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(12, 70)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(268, 23)
Me.Button3.TabIndex = 2
Me.Button3.Text = "Button3"
Me.Button3.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.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(ByVal sender As Object, ByVal e As
EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
Dim t As New Thread(AddressOf ShowForm)
t.Start(DirectCast(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(Activator.CreateInstance(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()
InitializeComponent()
End Sub

Private Sub InitializeComponent()
Me.SuspendLayout()
'
'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()
InitializeComponent()
End Sub

Private Sub InitializeComponent()
Me.SuspendLayout()
'
'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()
InitializeComponent()
End Sub

Private Sub InitializeComponent()
Me.SuspendLayout()
'
'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
 
Back
Top