By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,903 Members | 1,410 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,903 IT Pros & Developers. It's quick & easy.

User Control and Threading Help

P: n/a
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!

Feb 28 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

teillon wrote:
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!

Mar 1 '06 #2

P: n/a
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?

Mar 1 '06 #3

P: n/a
"teillon" <te*****@mountainshadow.com> schrieb
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

Mar 3 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.