Like I said before, threading kicked my butt for a while... I probably still do things wrong too, but have routines that work for my needs and I'm
From the point you call 'thrMyProc.Start', you have just started a new thread that is running the procedure ' MyProcThread() ' . From that point you probably don't want to allow the user to either accidentally start another thread (say they click your button to probe to many times...), and you don't want to do anything in the calling routine 'MyProc' after starting the thread that depends on the thread, as they are potentially out of sync at that point.
I'm assuming you are calling MyProc direct from the button press event. what you might want to do to prevent a user from multiple runs of your routine is:
- Code: Select all
Public Sub MyProc()
if thrMyProc is nothing then ' its OK to start thread, existing not running
Dim thrMyProc As New Thread(Sub() MyProcThread())
thrMyProc.CurrentCulture = Thread.CurrentThread.CurrentCulture ' Stay in English
thrMyProc.Start()
else ' existing thread is running,
messagebox.show("Another threaded routine is currently running!")
' do nothing, or alternately could kill the running thread by setting abort flag and setting thread = nothing.
end if
End Sub
Then you let MyProcThread run to the end, call other subs, do what it wants, and when you terminate MyProcThread routine for what ever reason (either finish successfully, or aborted/canceled), you can throw the thrMyProc = nothing at the end or in the middle as needed:
- Code: Select all
Public Sub MyProcThread()
' Do a probe check
if probecheck = false then
exit sub
thrMyProc = nothing ' kill the thread due to aborting process...
end if
UC.Codesync(gcodestr)
While IsMoving() ' <-- this is the private IsMoving()
Thread.Sleep(5)
End While
Call StopCheck()
'Finished Succesfully!
thrMyProc = nothing
End Sub
if thrMyProc is defined globally, any subroutine you have can set it to nothing.
hope that helps more than confuses.
Just to confirm, I did not back-check any of the code I modified above, it could have typoes, etc.
regards
Eric