473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
Jun 27 '08 #1
2 2291
On Apr 24, 3:48*pm, "John" <i...@nospam.infovis.co.ukwrote:
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

--
Tom Shelton
Jun 27 '08 #2
Hi Tom

Many Thanks

Regards

"Tom Shelton" <to*********@comcast.netwrote in message
news:01**********************************@m36g2000 hse.googlegroups.com...
On Apr 24, 3:48 pm, "John" <i...@nospam.infovis.co.ukwrote:
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

--
Tom Shelton
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Wilson | last post by:
Is there a way to open an OLE DB database from within Access? I would like to use the Access GUI with its table and query explorer to examine a database only available through an OLEDB provider...
1
by: Alasdair | last post by:
Friends, I'm an old C programmer who upgraded to C++ but was never comfortable with it. I've recently moved to C# and love it but I obviously am missing some of the subtleties. I thought the...
3
by: mahtan | last post by:
Please help I have the problem that when I change the data in a Windows.Forms.DataGrid by a separate thread the following Exception is thrown: ThreadSystem.NullReferenceException in...
4
by: Scott Johnson | last post by:
Hi! Is there a way to "preload" a form using a thread or something else so that my user doesn't have to wait 5 seconds (initializing time) between forms? Some of these forms have tab strips with...
3
by: Per Dunberg | last post by:
Hi all, I have to develop a "skinned" application and I have a problem with the graphics. When a form is loaded and displayed there's aways a flicker where all the controls are located on the...
8
by: Stephen Rice | last post by:
Hi, I have a periodic problem which I am having a real time trying to sort. Background: An MDI VB app with a DB on SQL 2000. I have wrapped all the DB access into an object which spawns a...
6
by: A.Weinman | last post by:
Hello all, I have an application that has multiple forms, only 3 of which matter for this issue. There is a main form that starts invisible and does nothing more than start other forms and link...
6
by: John | last post by:
Hi I am trying to create (not open) several forms in the background threads using the code given below at the end. 1. Am I doing it correctly? 2. How can I get handle sot these forms in the...
2
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
I am (still) relatively new to Windows applications, most of my experience has been Web based, and I am confused about what exactly happens when the Main() method is called and how to manipulate...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.