Question Resuming a thread after AbortReset

IHIguy

New member
Joined
Nov 4, 2011
Messages
2
Programming Experience
3-5
Hello all,
I am trying to find a way to resume a thread after catching the AbortThreadException and calling AbortReset.

My thread is running a loop to control two motors used in a raster scan. Once the button is pressed to begin the scan, I want to be able to click a stop scan button and have the following happen:

1. AbortThreadException is thrown and caught- a window comes up asking if you really want to abort the scan. Click yes for abort, no for suspend, or cancel to continue scanning.

2. Given users answer the program responds accordingly- If cancel, then the scan should continue right where it left off and not restart.

My problem occurs in my understanding of the thread events. I am not sure how to tell a thread to wait for a button event nor do i know how to tell a thread to continue where it left off. I also noticed that the 'suspend' and 'resume' methods have been deemed obsolete and i am not sure of the alternative way of doing those actions.

The following is the code used to catch the exception and execute the task.


VB.NET:
Private Sub RasterScan()
 Try
      'My control loop for the two motors is here. 
      ' It is a For Next loop that scans and increments the two motors used for the scan.
            
        Catch e As ThreadAbortException
            Dim Result As Integer
            Result = MessageBox.Show("Scan stopped. Do you want to abort the scan?" & vbNewLine & _
                            "Click 'Yes' to abort, 'No' to Pause the scan or 'Cancel' to resume scanning." _
                            , "Scan Stopped", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
            Select Case Result
                Case 6 'Yes
                    MoveTo.Abort()
                Case 7 'No
                    'This should pause the scan and resume only when the user clicks a resume scan button.
                    'It should resume from where it left off. 
                Case Else ' Cancel
                    'This should continue the scan where it left off- not start from the beginning. 

            End Select

        End Try
        MessageBox.Show("Scan Complete", "Successful Scan")
        BeginScanButton.Invoke(Sub() BeginScanButton.Text = "Begin Scan")
        Thread.CurrentThread.Abort()
    End Sub

Any help or insight is much appreciated!
Thanks,
IHIguy
 
ThreadAbortException Class

As explained you can ResetAbort before end of Catch block. You can't tell a thread to "continue where it left off", only that the abort should be reset. It's up to you to determine and carry on what thread is supposed to do. If whatever you are doing has pause/resume operations they can be used.

Thread.Abort should be considered last resort to stop a thread, as it can put all objects used in an unstable state, as such resuming the thread and the objects in use after that is questionable.

You have the Result variable type wrong, return type of MessageBox.Show function is DialogResult and not Integer.
 
ThreadAbortException Class

You can't tell a thread to "continue where it left off", only that the abort should be reset. It's up to you to determine and carry on what thread is supposed to do. If whatever you are doing has pause/resume operations they can be used.

Thread.Abort should be considered last resort to stop a thread, as it can put all objects used in an unstable state, as such resuming the thread and the objects in use after that is questionable.

Thanks for your reply.

Ok, so what is the best way suspend or put the thread to sleep until another command is received from a button? I would be able to do this no problem but "suspend" and 'resume' are obsolete now- so i am at a loss on how to proceed.
Just to clarify- there is no way to tell the thread to pick up where it left off? I must come up with a way to save the current position, increment, etc. and re-use them next time the thread starts?

Changed Result variable type to DialogResult, thanks.

Regards,
IHIguy
 
Back
Top