473,406 Members | 2,713 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,406 software developers and data experts.

Threading with forms

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.

3. How do I know when threads have finished forms creation so I can show the
form if needed?

Thanks

Regards
Code
====

Private Sub Main()
Dim t1 As New Thread(AddressOf CreateForm)
t1.Start(GetType(Form1))

Dim t2 As New Thread(AddressOf CreateForm)
t2.Start(GetType(Form2))

Dim t3 As New Thread(AddressOf CreateForm)
t3.Start(GetType(Form3))
End Sub

Public Sub CreateForm(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)
End Using
End If
End Sub
Jun 27 '08 #1
6 1053
John,

Forms are not meant to be used as child threads.

A form is an UI which interact with a user, how would you let the user input
data assynchonosly, we have more fingers, but there is always only one form
active on your screen.

Be aware that multithreading cost more processing time then single trheading
as the threads need to be managed, therefore I don't see the sense in what
you try to do.

Cor

"John" <in**@nospam.infovis.co.ukschreef in bericht
news:OO**************@TK2MSFTNGP04.phx.gbl...
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.

3. How do I know when threads have finished forms creation so I can show
the form if needed?

Thanks

Regards
Code
====

Private Sub Main()
Dim t1 As New Thread(AddressOf CreateForm)
t1.Start(GetType(Form1))

Dim t2 As New Thread(AddressOf CreateForm)
t2.Start(GetType(Form2))

Dim t3 As New Thread(AddressOf CreateForm)
t3.Start(GetType(Form3))
End Sub

Public Sub CreateForm(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)
End Using
End If
End Sub
Jun 27 '08 #2
On 2008-05-04, John <in**@nospam.infovis.co.ukwrote:
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.

3. How do I know when threads have finished forms creation so I can show the
form if needed?

Thanks

Regards
Code
====

Private Sub Main()
Dim t1 As New Thread(AddressOf CreateForm)
t1.Start(GetType(Form1))

Dim t2 As New Thread(AddressOf CreateForm)
t2.Start(GetType(Form2))

Dim t3 As New Thread(AddressOf CreateForm)
t3.Start(GetType(Form3))
End Sub

Public Sub CreateForm(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)
' Add this to show the form
Application.Run(f)
End Using
End If
End Sub

Application.Run starts a new message pump on the current thread - and
shows the form. As for access to the forms after creation... Well, if
you intended to do that then you would need to increase the scope of you
form variables, and that would greatly increase the complexity of the
code. You would of course, have to make sure that all access to those
forms was marshaled correctly (using Invoke or BeginInvoke/EndInvoke) -
since as always, windows forms are NOT thread safe, and can not be
directly accessed from other threads.

Any logic on that thread would have to be in the form that is passed to
applciation.run, since it will not return until the form is closed - in
other words, it's a blocking method.

Can you explain exactly what you are trying to accomplish? There is
probably a simpler way of doing whatever it is your trying to do :)

--
Tom Shelton
Jun 27 '08 #3
"John" <in**@nospam.infovis.co.ukschrieb:
I am trying to create (not open) several forms in the background threads
using the code given below at the end.
I suggest to open all form's in the application's main (UI) thread and use
'Control.InvokeRequired', 'Control.BeginInvoke', and 'Control.Invoke' to
access the controls/forms from within the other threads.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jun 27 '08 #4
I have several forms and clients are complaining that they are sick of time
that it takes to open one form after another. I was hoping that after first
form is loaded the rest can start loading in background without being
visible. That way when user needs them they are already loaded and become
visible quicker.

Thanks

Regards

"Tom Shelton" <to*********@YOUKNOWTHEDRILLcomcast.netwrote in message
news:uo**************@TK2MSFTNGP05.phx.gbl...
On 2008-05-04, John <in**@nospam.infovis.co.ukwrote:
>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.

3. How do I know when threads have finished forms creation so I can show
the
form if needed?

Thanks

Regards
Code
====

Private Sub Main()
Dim t1 As New Thread(AddressOf CreateForm)
t1.Start(GetType(Form1))

Dim t2 As New Thread(AddressOf CreateForm)
t2.Start(GetType(Form2))

Dim t3 As New Thread(AddressOf CreateForm)
t3.Start(GetType(Form3))
End Sub

Public Sub CreateForm(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)

' Add this to show the form
Application.Run(f)
> End Using
End If
End Sub


Application.Run starts a new message pump on the current thread - and
shows the form. As for access to the forms after creation... Well, if
you intended to do that then you would need to increase the scope of you
form variables, and that would greatly increase the complexity of the
code. You would of course, have to make sure that all access to those
forms was marshaled correctly (using Invoke or BeginInvoke/EndInvoke) -
since as always, windows forms are NOT thread safe, and can not be
directly accessed from other threads.

Any logic on that thread would have to be in the form that is passed to
applciation.run, since it will not return until the form is closed - in
other words, it's a blocking method.

Can you explain exactly what you are trying to accomplish? There is
probably a simpler way of doing whatever it is your trying to do :)

--
Tom Shelton

Jun 27 '08 #5

"John" <in**@nospam.infovis.co.ukwrote in message
news:uO**************@TK2MSFTNGP03.phx.gbl...
>I have several forms and clients are complaining that they are sick of time
that it takes to open one form after another. I was hoping that after
first form is loaded the rest can start loading in background without being
visible. That way when user needs them they are already loaded and become
visible quicker.
Forms load is pretty fast. It's what you're doing during the form load that
can slow things down. And sometimes one has to give the illusion of speed by
not loading all the table rows into a datagrid but load a subset of data
during the form load and other things one can do to give the illusion of
speed.

http://msdn.microsoft.com/en-us/magazine/cc163630.aspx

Jun 27 '08 #6
John wrote:
I have several forms and clients are complaining that they are sick
of time that it takes to open one form after another. I was hoping
that after first form is loaded the rest can start loading in
background without being visible. That way when user needs them they
are already loaded and become visible quicker.
It is probably the retrieval of data that is slow, not the form itself. You
could try loading datasets in background, then hook them up to the form when it
is opened.
Jun 27 '08 #7

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

Similar topics

8
by: Mahesh Devjibhai Dhola [MVP] | last post by:
We are building Chat like application using Forms and as a result our programming is becoming complicated to display messages received on different threads in the chat window (due to STA...
13
by: RCS | last post by:
I have a UI that needs a couple of threads to do some significant processing on a couple of different forms - and while it's at it, update the UI (set textboxes, fill in listviews). I created a...
2
by: Tyson Ackland | last post by:
I have written a very simple threading proggie in an attempt to teach myself threading. I have seen it referred to in articles that Forms are not thread safe. My form has two labels which are...
0
by: Eric Sabine | last post by:
OK, I'm trying to further my understanding of threading. The code below I wrote as kind of a primer to myself and maybe a template that I could use in the future. What I tried to do was pass data...
7
by: Terry Olsen | last post by:
I run this code: Private Sub p_recv(ByVal sender As Object, ByVal e As SerialReceivedEventArgs) Handles p.ReceivedEvent txtRecv.Text += p.ReadExisting End Sub I get this error: ...
0
by: Colmeister | last post by:
I recently read Jason Clark's excellent article on Unhandled Exceptions (http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx) and have attempted to incorporate the features he talks...
6
by: hzgt9b | last post by:
Using VS 2003, .NET: I developed a windows application that performs several actions based on an input file. The application displays a progress bar as each action executes. Based on new...
7
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has...
19
by: frankiespark | last post by:
Hello all, I was perusing the internet for information on threading when I came across this group. Since there seems to be a lot of good ideas and useful info I thought I'd pose a question. ...
9
by: tshad | last post by:
I have a Windows App that is doing some work and then writing a "Now Processing..." line to the status line of the window as well as the Textbox on the form. But the problem is that the work is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.