How to wait until all tasks finished without blocking UI thread?

Pablo

In the following code I disable button before processing tasks and would like to enable it after all tasks are finished.

List<Task> tasks = new List<Task>();
buttonUpdateImage.Enabled = false; // disable button
foreach (OLVListItem item in cellsListView.CheckedItems)
{
    Cell c = (Cell)(item.RowObject);

    var task = Task.Factory.StartNew(() =>
    {
        Process p = new Process();
        ...
        p.Start();
        p.WaitForExit();
    });
    task.ContinueWith(t => c.Status = 0);
    tasks.Add(task);
}

Task.WaitAll(tasks.ToArray());
// enable button here

WaitAll is blocking the UI thread. How can I wait until all tasks finish and then enable the button?

Yuval Itzchakov

First, I'd install Microsoft.Bcl.Async which will enable the use of async-await in .NET 4.0.

Now, using the answer to this question, you can asynchronously register for process exit, with no need to use Task.Factory.StartNew:

public static class ProcessExtensions
{
    public static Task RunProcessAsync(this Process process, string fileName)
    {
        if (process == null)
            throw new ArgumentNullException(nameof(process));

        var tcs = new TaskCompletionSource<bool>();
        process.StartInfo = new ProcessStartInfo
        {
            FileName = fileName 
        };

        process.EnableRaisingEvents = true
        process.Exited += (sender, args) =>
        {
            tcs.SetResult(true);
            process.Dispose();
        };

        process.Start();
        return tcs.Task;
    }
}

Now, you can do this:

buttonUpdateImage.Enabled = false; // disable button

var tasks = cellsListView.CheckedItems.Cast<OLVListItem>()
                                      .Select(async item => 
{
    Cell cell = (Cell)item.RowObject;

    var process = new Process();
    await process.RunProcessAsync("path");

    cell.Status = 0;
});

await Task.WhenAll(tasks);
buttonUpdateImage.Enabled = true;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to wait until all tasks are finished before running code

From Dev

How to wait for thread to complete without blocking UI

From Dev

Launch an application and wait until it has finished without blocking redraw

From Java

How to wait until all async calls are finished

From Dev

How to wait until all NSOperations is finished?

From Dev

Wait for Task to Complete without Blocking UI Thread

From Dev

Shutdown Java Executor After All Submitted Tasks Finished Without Blocking

From Dev

Shutdown Java Executor After All Submitted Tasks Finished Without Blocking

From Dev

Wait until my thread is finished

From Dev

How to wait for a return value from an event handler without blocking main UI thread

From Dev

How to wait until an animation is finished in Swift?

From Dev

how to wait until a web request with HttpWebRequest is finished?

From Dev

How to wait until some jQuery methods are finished?

From Dev

How to wait until networking by Alamofire has finished?

From Dev

How to wait until my batch file is finished

From Dev

How to get jQuery to wait until an animation is finished?

From Dev

Android - How to wait until a SnapshotListener has finished?

From Dev

Synchronized, lock and wait blocking main UI thread

From Dev

Run thread from Tkinter and wait until it's finished

From Dev

Run thread from Tkinter and wait until it's finished

From Dev

How to create a timer running in the background without blocking the UI thread with Xamarin?

From Dev

How do I transfer an Android asset without blocking the UI thread?

From Dev

C# .Net - How to make application wait until all threads created in Library are finished

From Dev

Wait until function is finished

From Dev

Wait for DataReceived to fire without blocking the ui

From Dev

How do I block the current thread until OnComplete has finished executing without the use of traditional threading primitives?

From Dev

ExecutorService's shutdown() doesn't wait until all threads will be finished

From Dev

Create threads in loop and wait until all threads finished/aborted

From Dev

Run a thread from UI thread without blocking UI thread

Related Related

  1. 1

    How to wait until all tasks are finished before running code

  2. 2

    How to wait for thread to complete without blocking UI

  3. 3

    Launch an application and wait until it has finished without blocking redraw

  4. 4

    How to wait until all async calls are finished

  5. 5

    How to wait until all NSOperations is finished?

  6. 6

    Wait for Task to Complete without Blocking UI Thread

  7. 7

    Shutdown Java Executor After All Submitted Tasks Finished Without Blocking

  8. 8

    Shutdown Java Executor After All Submitted Tasks Finished Without Blocking

  9. 9

    Wait until my thread is finished

  10. 10

    How to wait for a return value from an event handler without blocking main UI thread

  11. 11

    How to wait until an animation is finished in Swift?

  12. 12

    how to wait until a web request with HttpWebRequest is finished?

  13. 13

    How to wait until some jQuery methods are finished?

  14. 14

    How to wait until networking by Alamofire has finished?

  15. 15

    How to wait until my batch file is finished

  16. 16

    How to get jQuery to wait until an animation is finished?

  17. 17

    Android - How to wait until a SnapshotListener has finished?

  18. 18

    Synchronized, lock and wait blocking main UI thread

  19. 19

    Run thread from Tkinter and wait until it's finished

  20. 20

    Run thread from Tkinter and wait until it's finished

  21. 21

    How to create a timer running in the background without blocking the UI thread with Xamarin?

  22. 22

    How do I transfer an Android asset without blocking the UI thread?

  23. 23

    C# .Net - How to make application wait until all threads created in Library are finished

  24. 24

    Wait until function is finished

  25. 25

    Wait for DataReceived to fire without blocking the ui

  26. 26

    How do I block the current thread until OnComplete has finished executing without the use of traditional threading primitives?

  27. 27

    ExecutorService's shutdown() doesn't wait until all threads will be finished

  28. 28

    Create threads in loop and wait until all threads finished/aborted

  29. 29

    Run a thread from UI thread without blocking UI thread

HotTag

Archive