Answer the question
In order to leave comments, you need to log in
How to avoid running an async method multiple times at the same time and keep progress notifications?
There is a Task UpdateDataAsync(IProgress progress, CancellationToken ct) method that updates the data in the application. It can be called either by a timer in the background, or explicitly by the user. It runs for about a minute.
It is possible that the method was launched in the background and after that the user also launched the update. In this case, you need to:
1. Do not create a new Task, but wait for the one that is already running
2. Notify both the user and the background service about progress
3. Keep the ability
to
cancel UpdateDataAsync progress and ctare put into an internal list, then progress.Report is called in the body of the method for each saved progress. The same is true for CancellationToken.
Doesn't look good, in my opinion. Judging by the fact that Google did not give results, such tasks are solved in a different way. What do you think?
Answer the question
In order to leave comments, you need to log in
If it's in the same process, the code is trivial
if(IsBusy){
return;
}
IsBusy = true;
await UpdateDataAsync ();
IsBusy = false;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question