(1) For the Abort method of the Thread, if the Thread is currently executing an unmanaged code, the CLR will not throw ThreadAbortException. Only when the Code continues to return to the CLR, threadAbortException. Of course, even ThreadAbortException In the CLR environment will not be immediately triggered.
(2) For the CancelAsync method of BackgroundWorker, you need to set the workersuppscanscancellation attribute to True. The CancellationPending ID is detected within the execution method, and the user is responsible for exiting.
(3) For CancellationTokenSource, the scenario mainly sets an expected execution time for the task and automatically cancels the time-out task. The Cancel method is automatically triggered after the time interval is reached. IsCancellationRequested is set to True. You also need to check the IsCancellationRequested attribute within the method.
Based on the above, the Retry (re-execute) Operation of the method may take a long time, which may easily lead to blocking of the main thread. Therefore, this article mainly uses BackgroundWorker to execute related operations. The RetryProvider class diagram and related operation example diagram are as follows:
RetryProvider is mainly used to re-Execute methods and control and process the execution methods in the backend thread BackgroundWorker. It mainly provides the following two methods:
(1) public void StartAsync (Action target, int waitTimeout = 0, int retryCount = 1)
(2) public void StartAsyncFunc (Func <bool> target, int waitTimeout = 0, int retryCount = 1)
The first method is mainly for methods without return parameters. The second method is mainly for methods that return bool parameters. Of course, you can extend this class to support other related parameter methods. The method is as follows:
StartAsync(Action target, waitTimeout = , retryCount = StartAsyncFunc(Func<> target, waitTimeout = , retryCount = StartAsyncRetry( target, waitTimeout, (target == ArgumentNullException( (._backgroupThread == ._backgroupThread = ._backgroupThread.WorkerSupportsCancellation = ._backgroupThread.WorkerReportsProgress = ._backgroupThread.DoWork +=._backgroupThread.ProgressChanged += (.WaitTimeout =.RetryCount =
The method executed in the background thread mainly controls and processes the CancellationPending attribute of RetryCount and BackgroundWorker, and reports the progress based on the number of method retries. The Code is as follows:
Start( (target == retryCount = ._backgroupThread.ReportProgress( (! (.PerRetryBegin != .PerRetryBegin(.RetryCount - (target.GetType() == ((target Func<> InvalidOperationException( (.PerRetryFailedCompleted != ._backgroupThread.ReportProgress((.RetryCount - retryCount + ) * / (.PerRetryEnd != .PerRetryEnd(.RetryCount - (.RetryCount > -- (retryCount == ( (.Cancelled !=
Currently, only retry methods for the Action and Func <bool> parameters are provided. Therefore, you only need to check the parameter type (such as target. getType () = typeof (Action. If the input parameter is of the Func <bool> type, the current processing method is: Check whether the method is successfully executed. if true is returned, that is, if (target as Func <bool> ). invoke (), the backend thread of the retry operation is exited, And the retry operation is not performed. Otherwise, an exception is thrown to continue the operation.
Thread. Sleep (this. WaitTimeout) is mainly used to block the current Thread, wait for the set Timeout time, and then continue to execute related methods.
(1) Start the retry method and set relevant parameters and related events.
btnStart_Click( waitTimeout = .TryParse(.nupWaitTimeout.Value.ToString(), retryCount = .TryParse(.nupRetryCount.Value.ToString(), Start( waitTimeout, (._retryProvider == ._retryProvider = ._retryProvider.PerRetryFailedCompleted +=._retryProvider.Cancelled +=._retryProvider.PerRetryBegin +=._retryProvider.PerRetryEnd +=._retryProvider.ProgressChanged +=._retryProvider.Completed += (.btnStart.Enabled = (.listBoxRecord.Items.Count > ._retryProvider.StartAsyncFunc(ThrowExceptionMethod, waitTimeout *
The corresponding event is as follows:
Public delegate void CompletedEventHandler (bool success); // RetryProvider completes Delegation
Public event CompletedEventHandler Completed; // RetryProvider completes the event
Public delegate void CancelledEventHandler (); // RetryProvider cancel Delegation
Public event CancelledEventHandler Cancelled; // RetryProvider cancels the event
Public delegate void PerRetryBeginEventHandler (int retryIndex); // RetryProvider the number of retries of the operation starting from 0.
Public event PerRetryBeginEventHandler PerRetryBegin; // RetryProvider
Public delegate void PerRetryEndEventHandler (int retryIndex); // RetryProvider the number of retries at the end of each retry. The number of retryIndex retries starts from 0.
Public event PerRetryEndEventHandler PerRetryEnd; // RetryProvider
Public delegate void PerRetryFailedEventHandler (Exception ex); // RetryProvider indicates the Exception message of the failed parameter Exception
Public event PerRetryFailedEventHandler PerRetryFailedCompleted; // RetryProvider
Public delegate void ProgressChangedEventHandler (int percent); // RetryProvider progress change delegate
Public event ProgressChangedEventHandler ProgressChanged; // RetryProvider progress change event
(2) cancel the retry operation:
btnCancel_Click( (._retryProvider !=
This method will cause the background thread to execute the CancelAsync operation. In the retry operation, if the CancellationPending of the background thread is detected to be true, the Cancelled event is triggered and the background thread is exited.
RetryProvider encapsulates retry operations. Considering that the retry operation may take a long time, BackgroundWorker is used for corresponding processing and control. This is mainly used to control the execution of operations. For uncontrollable cases, such as calling a third-party program to perform corresponding operations (such as calling MATLAB for command operations), the waiting time and execution time are uncertain, the best way is to use Timer to trigger the progress bar to scroll cyclically. The background thread executes the corresponding operations and then sets the corresponding content after the thread completes execution.
: Retry the solution file