I've been able to write a thread into my app which basically does a loop in the background perfroming detection for a device being added or removed.
works great!

i am trying to implement another thread to check for a file on the found device...but i still haven't figured out threading enough to make this work, the versions i try to implemt are either looping along with the initial loop or cancelling itself because its already running.

i've been trying to put a variable in that would switch the thread off, but it doesn't seem to work.

i'm not necessarily looking for the code to use but more the logic i should use to make this work.

4 Years
Discussion Span
Last Post by flywheeljack

Got my threading to work, so much so that i was able to implement 4 different threads! each doing something different.

So it turns out that when working with threads, at least as far as i found you need to understand how a thread is assembled, what it is capable of doing, how it communicates, how it starts and how you can shut it down.

First is basic assembly, a thread uses a class that is made up of several interacting pieces - for example some parts of the class will refer to your calls to the thread from another form

Public Class DevThreadClass

'sets the variables to be used by the class

Private MyThreadIndex As Integer

Private MyDevStatus As Integer = 0

Private MyArgs(1) As Object

Private MyMainWindow As Form

'sets the delegate to use for passing information from thread to form

Private Delegate Sub DevNotifyMainWindow(ByVal ThreadIndex As Integer, ByVal MyDevStatus As Integer)

Private MyDevNotifyMainWindow As DevNotifyMainWindow

...so you have FORM 1 which is where a lot of your program resides this is where you put your tangible controls like text boxes etc. But to run a thread to fill the text box for example, you have to refer to a thread class designed to perform a non-tangible operation (sorry if my word usage here throws some people off...)

your thread performs a search or an operation, but doesn't directly produce the result.

Thats the job of the delegate...the delegate is the go-between for the thread and the FORM 1 where you actually want something to happen.

So for my examples the thread searches for a condition, then sends a message back thru the delegate , on the main form the code is told that if the delegate sends this message/or that message then do such and such...and thats it.

Public Sub DevReceiveThreadMessage(ByVal ThreadIndex As Integer, ByVal MyDevStatus As Integer)

    If MyDevStatus = 1 Then

        USBindicator.BackColor = Color.Green


        XmlFile = 1

        Label1.Text = 0

My biggest problem with threads was wrapping my mind around how they actually functioned, after that it was just a matter of practice.

For example i then learned to switch the threads i needed on / off when necessary by using the abort command.

And to start up a thread that was previously aborted use New System.Threading.Thread, so that the system can actually start a new thread using the threads address in your thread class.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.