Threading...

  • Thread starter Thread starter Morgon
  • Start date Start date
M

Morgon

Guest
Has anyone figured out the mystical nature of threading yet? lol..

I have a Class that I've created, that starts a new thread ... I'd like to be able to control this thread explicitly beyond my class.. (Basically, I have a button that starts a background thread.. and the Sub it calls, is by whatever is in a listbox) .. That part works fine. But I have another button that's supposed to stop the thread.. and I would like for it to stop the thread based on what's highlighted in the listbox.. but instead it stops the master thread and closes the program.

Anyone able to help me on this? Maybe I'm being a little too confusing, if so, just let me know and I'll explain further.

Thank you!
-Morgon
 
You'll have to keep a reference to the thread object you create, then use that to stop it. If the class creates the thread, perhaps you could return the thread object in the function that creates it or something?
 
I'm going to assume that your listbox and button are members of the same class. So, all you need to do is declare a class level variable, like so:

Code:
Class myForm
     Private Shared myThreadDelegate As New ThreadStart(AddressOf ThreadWork.DoWork)
     Private Shared myThread As New Thread(myThreadDelegate)
End Class [color=green]'myForm[/color]

You can then access your thread from any sub or function within the class, including your listbox and button event handlers. However, if your thread creation code resides in another class you'll need take divil's advice and pass references as needed.

Good Luck
-CL
 
Pardon my ignorance guys... but.. yeah, I'm kind of the n00b type..

How would I know if my listbox and button are members of the same class? ... They're on the same form .. but other than that, I have no idea how I would go about checking that, or adding them to the same class.

Let me provide you with some code so maybe I can help you guys help me :-D

Code:
    Class CaptureClass
        Public Variable1 As String
        Public Variable2 As String

        Public Sub CapCam()
            While (Thread.CurrentThread.IsAlive)

                ... Non-related Code ...
                        Thread.Sleep(1000) 'Take a short commercial break
            End While
        End Sub
    End Class

...

    Public Sub CaptureArea(ByVal Cam As String)
        Dim CO As New CaptureClass()
        Dim tCC As New Thread(AddressOf CO.CapCam)

            ... other non-related code here ...

tCC.Start()
End Sub

....

 Private Sub StartCap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartCap.Click
Dim Cam As String = Listbox1.SelectedItem
        CaptureArea(Cam)

End Sub

Messy I'm sure.. but that's how it is right now, til I can learn some good VB coding techniques (Speaking of.. just running my 32k program takes up 11 Megs?? Someone kindly explain that please. And doing ANYTHING makes the memory usage go up by 8k... clicking buttons, selecting things on the listbox, etc... is that just the nature of VB, or have I horribly screwed up?)

As always.. THANK YOU!! :)
-Morgon
 
Back
Top