473,668 Members | 2,456 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(AddressO f CreateForm)
t1.Start(GetTyp e(Form1))

Dim t2 As New Thread(AddressO f CreateForm)
t2.Start(GetTyp e(Form2))

Dim t3 As New Thread(AddressO f CreateForm)
t3.Start(GetTyp e(Form3))
End Sub

Public Sub CreateForm(ByVa l 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)
End Using
End If
End Sub
Jun 27 '08 #1
6 1062
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.in fovis.co.ukschr eef in bericht
news:OO******** ******@TK2MSFTN GP04.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(AddressO f CreateForm)
t1.Start(GetTyp e(Form1))

Dim t2 As New Thread(AddressO f CreateForm)
t2.Start(GetTyp e(Form2))

Dim t3 As New Thread(AddressO f CreateForm)
t3.Start(GetTyp e(Form3))
End Sub

Public Sub CreateForm(ByVa l 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)
End Using
End If
End Sub
Jun 27 '08 #2
On 2008-05-04, John <in**@nospam.in fovis.co.ukwrot e:
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(AddressO f CreateForm)
t1.Start(GetTyp e(Form1))

Dim t2 As New Thread(AddressO f CreateForm)
t2.Start(GetTyp e(Form2))

Dim t3 As New Thread(AddressO f CreateForm)
t3.Start(GetTyp e(Form3))
End Sub

Public Sub CreateForm(ByVa l 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)
' 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.in fovis.co.ukschr ieb:
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.Invoke Required', 'Control.BeginI nvoke', 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*********@YO UKNOWTHEDRILLco mcast.netwrote in message
news:uo******** ******@TK2MSFTN GP05.phx.gbl...
On 2008-05-04, John <in**@nospam.in fovis.co.ukwrot e:
>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(AddressO f CreateForm)
t1.Start(GetTyp e(Form1))

Dim t2 As New Thread(AddressO f CreateForm)
t2.Start(GetTyp e(Form2))

Dim t3 As New Thread(AddressO f CreateForm)
t3.Start(GetTyp e(Form3))
End Sub

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

' 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.in fovis.co.ukwrot e in message
news:uO******** ******@TK2MSFTN GP03.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
1898
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 requirements of Forms). Is there a way to build Chat like application WITHOUT using FORM, so that one can use free threading model (The link...
13
371
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 base class for the worker class, and made up some functions/delegates to handle the invoke stuff for the UI and that was fine for a prototype. I rewrote this chunk, broke things out into different classes - but the threading is still the same - and...
2
1319
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 written to by different threads in my example. It works fine so I'm wondering if anyone can tell me what I need to watch out for with respect to Forms and threading? For what it's worth, here is my short proggie: Private Sub SomeTask() ' This...
0
1478
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 into a background thread and get other data out and also update the main thread on which the main form was created. It seems to work fine. The basic function of the app is cheesy, I didn't spend any time on exception handling. northwind.mdb...
7
4059
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: --------------------ERROR TEXT-------------------- Illegal cross-thread operation: Control 'txtRecv' accessed from a thread
0
1983
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 about in a new application I'm writing. However, when I try to use ThreadStart to do some work in a separate thread from my GUI, the methods Jason described don't seem to catch the exception. Take the following source code: Public Class...
6
1892
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 requirements, this application needs to be able to shell off other processes and wait while in the mean time displaying a progress bar of the process's. I am using the System.Disgnostics.Process class to "start" and "waitForExit" of these processes... I...
7
2368
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 finished. I am trying to follow this example : http://www.codeproject.com/cs/miscctrl/progressdialog.asp But although the messages still get moved, the progress window never does anything. Here is my code in full, if anybody who knows...
19
1794
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. Threading is a new concept for me to implement. Here is my problem. I have a system that receives xml files and records their file locations in a database. I can potentially receive thousands,
9
4107
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 in another class from the main class. So it couldn't access the Status Line or textbox. So what we did was set them up as properties: string IStatusDisplay.Status
0
8459
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
8371
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8652
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7391
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
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
5677
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
4202
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...
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1779
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.