We have a single long(ish) operation in our app, Calculate
, which takes about 5 seconds to complete. This post is basically identical to what I am doing, but only considers completed threads. Anything that triggers Calculate
should kill any running calc and then restart it. It seems, based on posts like this one, that a ThreadPool
could solve this, but might not be the appropriate solution in these one-off cases.
So my question is how to properly dispose of the "old one" if it's still going. I am currently doing...
If CalcThread IsNot Nothing Then
CalcThread.Abort()
CalcThread = Nothing
End If
If CalcThread Is Nothing Then 'one assumes this will always be true
CalcThread = New Threading.Thread(AddressOf InternalCalculate)
CalcThread.IsBackground = True
End If
CalcThread.Start()
This appears to work, but is this the correct solution?
Update: Given that my use-case is that there is only a single one of these threads, and, as Chris notes below, Abort might not do that immediately, should I be using a SyncLock across the guts of Calculate
?