T
T
trauus2020-09-14 05:01:11
C++ / C#
trauus, 2020-09-14 05:01:11

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

1 answer(s)
V
Vladimir Korotenko, 2020-09-14
@firedragon

If it's in the same process, the code is trivial

if(IsBusy){
return;
}
IsBusy = true;
await UpdateDataAsync ();
IsBusy = false;

If it is 2 or more processes
1. create a file
2. write progress to it
3. destroy the file
4. when running in parallel, wait until the file is deleted

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question