Jump to content
Xtreme .Net Talk

Threading...


Recommended Posts

Guest Morgon
Posted

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

  • *Gurus*
Posted

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

Guest Morgon
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...