473,785 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2308
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
Jun 27 '08 #2
Hi Tom

Many Thanks

Regards

"Tom Shelton" <to*********@co mcast.netwrote in message
news:01******** *************** ***********@m36 g2000hse.google groups.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
Jun 27 '08 #3

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

Similar topics

3
3084
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 interface. I have seen several references on how to open an OLE DB database through VBA/ADO code but I wouldn't know how to go from there to actually have the database available in the Access GUI...
1
5071
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 following code would simply expose the Form1 and move on but what actually happens is that the Form1 load hangs while the Worker1 thread goes to sleep. It's not the Thread.Sleep that's causing the load problem... any code here executes but the...
3
2395
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 system.windows.forms.dll I can't catch this Exception and it only occurs when I
4
9917
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 lots of controls and database queries on them and I would like to try to have some(all) of the preloading of the next form and controls done in the background while a user is still entering data. (For example, my user visits screen A then B then...
3
3531
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 form. It seems like the controls erase the background and this cause a flicker everytime a form i loaded. When I hide and show forms that are already loaded there's no flicker, it's just when the form is loaded the first time.
8
2738
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 thread to access the database and then displays a modal dialog which allows the user to cancel the task, if it is taking longer than they want, and shows them a display of how long the query has been running so far.
6
2393
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 them all together. I also have a Login form and a View form, both started by the main form. My goal is to have the Login form pop up as soon as the application is started, and at the same time have the View form start loading data, but stay...
6
1066
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 calling sub so I can access these forms after creation.
2
4234
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 forms opening & closing. An example of this issue is as follows. I have a logon form open as the first thing. The main functional form opens when a user has successfully logged on. From the main form, a user should be able to logout which will...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10329
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10152
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.