Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

threading forms 1

Status
Not open for further replies.

jcisco

Programmer
Sep 17, 2002
125
US
I'm having a problem with threading forms, and i was wondering if someone can point me in the right direction.

I have two forms. 1 and 2.
on form 1 i have, form2 has no added code.

Private f As Form2
Private thrd As Threading.Thread

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
thrd = New Threading.Thread(New System.Threading.ThreadStart(AddressOf thrdStrt))
thrd.Priority = Threading.ThreadPriority.BelowNormal
thrd.Start()
End Sub

Private Sub thrdStrt()
f = New Form2
f.TopMost = True
f.Show()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(thrd.ThreadState.ToString)
End Sub

Now this works, but it works only for like a split second when the program runs. when i click on the button to check the state of the thread i get

The thread '<No Name>' (0x880) has exited with code 0 (0x0).

But I never stopped the thread i wanted it to keep running the second form. the only time it should stop is when the second form closes. How is this accomplished?

cheers.
john

--------------
:)
 
The thread stopped when it reached the End Sub of the thrdStrt.
Private Sub thrdStrt()
f = New Form2
f.TopMost = True
f.Show()
End Sub <---------- Thread ends

Change f.Show() to f.ShowDialog()



Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
jcisco -
You should be aware that the forms engine in .NET is still single-threaded. I don't know about VB.NET, but in C# the Main method explicitly says it's a single-threaded apartment.

You can for sure start threads to go do some I/O or CPU intensive task, but don't expect to update any GUI widgets while the other thread is running (like updating a progress bar) unless you take care to use a critical section.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
You can create a form on a separate thread from other forms but as stated below, all interaction with the form or its controls MUST occur on the thread that created the form. I have a muti-threaded application that creates as many as 10 simutaneous threads, each of which creates a form in debugging mode to display the XML returned by a call. Each form is updated only on the thread that created it and does not interact with the other forms or threads.

&quot;NET Framework Developer's Guide

Multithreaded Windows Forms Control SampleSee Also
Developing Windows Forms Controls
Language
C#

Visual Basic

Show All
Windows Forms uses the single-threaded apartment (STA) model because Windows Forms is based on native Win32 windows that are inherently apartment threaded. The STA model implies that a window can be created on any thread, but it cannot switch threads once created, and all function calls to it must occur on its creation thread. Outside Windows Forms, classes in the .NET Framework use the free threading model. For information about threading in the .NET Framework, see Threading.

The STA model requires that any methods on a control that need to be called from outside the control's creation thread must be marshaled to (executed on) the control's creation thread. The base class Control provides several methods (Invoke, BeginInvoke, EndInvoke) for this purpose. Invoke makes synchronous method calls; BeginInvoke makes asynchronous method calls.&quot;

Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top