Home All Groups Group Topic Archive Search About

User Control and Threading Help

Author
28 Feb 2006 11:18 PM
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!

Author
1 Mar 2006 3:14 AM
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

teillon wrote:
Show quoteHide quote
> 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!
>
Author
1 Mar 2006 8:21 PM
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?
Author
3 Mar 2006 10:17 AM
Armin Zingler
"teillon" <teil***@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