Guest Morgon Posted June 26, 2002 Posted June 26, 2002 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 Quote
*Gurus* divil Posted June 26, 2002 *Gurus* Posted June 26, 2002 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? Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Gurus* Derek Stone Posted June 26, 2002 *Gurus* Posted June 26, 2002 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: 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 Quote Posting Guidelines
Guest Morgon Posted June 27, 2002 Posted June 27, 2002 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 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.