New Methods: Implementing INFINITE LOOP which can be STOPPED upon request
.net, async-await, asynchronous, c#, multithreading
Solution
You should use `CancellationTokenSource`. But don't pass the `Token` to the `Task.Run` method and don't throw an exception. Simply check if cancellation was requested and then stop.
CancellationTokenSource cts = new CancellationTokenSource();
CancellationToken ct = cts.Token;
Task.Run(
async () =>
{
while (!ct.IsCancellationRequested)
{
await ConnectAsync();
await ReadAsync();
await WriteAsync();
await DisconnectAsync();
}
});
cts.Cancel();
When you pass a `CancellationToken` to the Task itself(`Task.Run(async () => ..., ct)`), you can't use it inside the delegate. It will only cancel the task if cancellation was called before the task started running. After it has begun, calling `Cancel` won't have any effect.
Problem
What is the correct method to implement a infinitely running task which executes an Async `BigJob()`? and can be stopped upon request Hint: I am trying to learn [a] new method(s) to update my existing strategy. I have this simple program (a tester) which has an Start and Stop button. When Start pressed, I am starting the tester which is going to find and test available devices in range and test the hell our of them in infinite amount of rounds until the user presses the Stop button. The important point is, the main process/action/bigJob() is and awaitble asynchronous process. So I would pseudo code the whole thing as ``` Start Round 1 starts Async Main job starts Async connect Async Read Async Write Async disconnect Nobody cancelled me yet Round 1 finishes Round 2 starts . . . Round 2 finishes Round 3 starts . Stop pressed, hence break out. Stop ``` So I am using `BackgroundWorker` to implement the infinite loop and `Async/Await` for the Connect, read, write and disconnect that I have written in `C#` under `.Net 4.5` Since my sub-tasks such as Connect and .etc are `async` then my main task is `async` and pressing the Stop button stops my process since it eliminates the infinite `while` loop that I have with having ``` while (!bw.CancellationPending) { await MainTask(); ... } ``` but it doesn't fire the `BackgroundWorker_RunWorkerCompleted` event, which doesn't do me any harm HOWEVER it's killing me since the background worker doesn't work as it's supposed to and I just keep thinking "there should be a better way!". So, I have been reading about so many new ideas, like why to use `BackgroundWorker` at all now that we have `Async/await`. Some were saying use `Task.Run()` it's magical. I even read recommendations and blogs about this godsend TPL Dataflow that I have never heard of. And so far I haven't found a method which is as structured, documented and layed-out as a `BackgroundWorker`. (unless it's such simple or a one liner which doesn't even need documentation or .etc) So, guys, what is the solution for this. Or better way to ask: What is the correct method to implement a infinitely running task which executes an Async `BigJob()`? and can be stopped upon request