Hi folks,
I have an application which creates threads to do some real-world work.
If I want to make the real-world work stop/be cancelled obv I would like the thread to stop ASAP so that problems are not caused when the user connects or disconnects things from the system being controlled.
What is the best way to do this?
I know the use of thread.abort() is discouraged unless implemented with all the proper considerations. So I did take a look at what some of these considerations may mean for my code and things started to get a bit complex, but I am not averse to retying this if there is no neater way.
The work the threads are doing is not cyclical, they do the their work once then finish, so having some kind of cancellation flag that is polled as part of a loop is not relevant.
I just started looking at cancellation tokens, but I they look like they require polling as well
My threads are started as in this code:
And the thread work itself looks something like the following psuedo-code:
Thanks, w
I have an application which creates threads to do some real-world work.
If I want to make the real-world work stop/be cancelled obv I would like the thread to stop ASAP so that problems are not caused when the user connects or disconnects things from the system being controlled.
What is the best way to do this?
I know the use of thread.abort() is discouraged unless implemented with all the proper considerations. So I did take a look at what some of these considerations may mean for my code and things started to get a bit complex, but I am not averse to retying this if there is no neater way.
The work the threads are doing is not cyclical, they do the their work once then finish, so having some kind of cancellation flag that is polled as part of a loop is not relevant.
I just started looking at cancellation tokens, but I they look like they require polling as well
My threads are started as in this code:
VB Code:
Dim jobThread As New Thread(AddressOf DoJob) jobThread.Start(schedule.Item(keys(index)))
And the thread work itself looks something like the following psuedo-code:
VB Code:
Private Sub DoJob(ByVal job As Object) ' Get the related context object using the key in the job argument ' Run the context object's .NextStep method ' This involves reading from a DB and opening, reading from, ' writing to and closing a TCP connection and getting and setting ' members from other threads. SyncLock(threadCollectionLock) ' Remove the reference to this thread from my thread collection End SyncLock End Sub