c# - Cancelling thread in async/await -
I'm trying to test a small app that is using ASC / using the transaction process More A waiting / async polling feature My test setup: My code (all in a class): When I run TestProcessTimerOnly () test, it will just sit there until I cancel the test rig at the end. When I run TestProcessTimerOnlyForcedCancellation () test, it behaves as expected. So the questions boil down: am I using the _cancellationToken converter properly? In one example, I start it with the at the start of time. In the second example I do not start it with any parameter. What am I doing wrong here? You are using what is highly disappointed. You are getting the job out of the Of course, when you make the What you probably want to do (IIUC) stores the transaction work without waiting for it and only wait when You can cancel the transaction (or expire):
[TestMethod] Public Zero TestProcessTimerOnly () {// This initializes and voting Up tp = The introduction of new TransactionProcessor (); Try {Thread. Sleep (5000); Tp.CancelProcessing (); } Hold (Exception Pre) {LogErrors (Ref TP, East); } Finally {DisplayLog (TP); }} [Testimede] Public Zero Test Process TimerOnly Forward Concern () {// It Introduces Pulling Verte TP = New Transaction Processor (1); Try {Thread. Sleep (5000); Tp.CancelProcessing (); } Hold (Exception Pre) {LogErrors (Ref TP, East); } Finally {DisplayLog (TP); }}
// Producer Public TransactionProcessor (integer debugForcedCancellationDelay = null?) {// & gt; & Gt; & Gt; & Gt; & Gt; & Gt; & Gt; & Gt; Cancel Setup & lt; & Lt; & Lt; & Lt; & Lt; & Lt; & Lt; & Lt; If (debugForcedCancellationDelay.IsEmpty () || debugForcedCancellationDelay.IsZeroOrLess ()) _cancellationToken = new cancellationTokenSource (); And _cancellationToken = new cancellation tokens (timespace.formsconds (debugforgar cancellation del.ly.le)); //> gt; & Gt; & Gt; & Gt; & Gt; & Gt; & Gt; & Gt; End & lt; & Lt; & Lt; & Lt; & Lt; & Lt; & Lt; & Lt; // was: // RepeatActionEvery ((= => TestingLog.Add ("Repeat action every 1 second"), TimeSpan.FromSeconds (1), _cancellationToken.Token) .Wait (); // Correct: // _processTask Type Function ... _processTask = RepeatActionEvery (() => is defined as a global field; TestingLog.Add ("repeat action every 1 second"), TimeSpan.FromSeconds ( 1), _cancellationToken.Token); //.stop(); } // was: // Cancel the public cancellation process () // true: cancel public async Task processing () {_cancellationToken.Cancel (); _processTask awaiting; } Public stable async Task RecurrenceActiveAvi (Action Action, Timespain Interval, Cancellation Token Cancellation) {While (true) {action}; Var function = function.dele (interval, cancellation token); Try {awaiting work; } Grip (Task Connectioned Exposure) {Return; }}}
What are you doing
duplication action which will only end when the cancellation token is canceled. But you are waiting synchronous ( blocking ) on that work, which means that you do not exit the constructor and the line to cancel the token (
tp.CancelProcessing ()) Arrive; ).
cancellation tokensource with one timeout, it will be canceled at the end of your start without it, so the task will be completed, the thread will wait It is free to finish the constructor and call
tp.CancelProcessing ();
What do you want to do
public transaction processor (int? DebugForcedCancellationDelay = null) {// ... Task = RepeatActionAir ((=) => Testing Add Lag ("Take action again every second"), Timespine. Foamseconds (1), _cancellationToken.Token); } Cancel Public async Task PricingSync () {_cancellationToken.Cancel (); Awaiting work; }
Comments
Post a Comment