User Control and Threading Help

T

teillon

I'm trying to write a VB.NET utility that performs a looping task--say,
list all files in a directory, over and over again, continuously.

I want to be able to put this logic in a User Control with a text box
for the path to monitor and a list box to display the results.

I want to be able to drop this control numerous times on a parent
project form and have them all run asyncronysly.

I am not a Threading expert, but I was assuming that each User Control
would run in it's own thread.

However, the results I'm getting show differently. If I have two
controls on the form and start one it runs fine, but when I start the
other one, the first one "pauses" until the second is stopped.

I know I need to do some threading here, but am having problems getting
it right. Is there a simple way to wrap the ENTIRE user control class
in it's own thread?

Thanks!
 
G

Gman

I don't think it works like that - every user control runs in the same
thread unless you explicitly tell it otherwise.

I've never used threading but you inspired me to have a pop at it.
Here's a demo that just populates a textbox on the main thread, it pops
up a message box -- so you can tell it's running in a separate thread:

'at top of module
Imports System.Threading

' place Button1 and TextBox1 on the form
'place this in Form class

Sub ThreadTest()
'make a new thread for our mySecondThread proc
Dim t As New Thread(New ThreadStart(AddressOf mySecondThread))
t.Start()
For i As Integer = 1 To 20
TextBox1.Text = TextBox1.Text & Now.ToLongTimeString _
& ControlChars.CrLf
TextBox1.Refresh()
'wait a bit
Thread.Sleep(200)
Next i
End Sub
Sub mySecondThread()
MsgBox("oooh")
MsgBox("ahhhh")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
ThreadTest()
End Sub

HTH
 
T

teillon

Any experts out there?

Is there no way to "simply" inherit the user control from the Thread
class and have it instantiate on the form in it's own thread?
 
A

Armin Zingler

teillon said:
Any experts out there?

Is there no way to "simply" inherit the user control from the Thread
class and have it instantiate on the form in it's own thread?


No. All windows, this includes controls, that have a relation to each other
(parent/child/owner), must be created in the same thread.

Controls don't run in threads. Code runs in threads. You can start a new
thread from each Usercontrol. If the thread is to update the Usercontrol,
call the control's Invoke or BeginInvoke method.


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top