How to pause task running on a worker thread and wait for user input?

ICloneable

If I have a task running on a worker thread and it finds something wrong, is it possible to pause and wait for the user to intervene before continuing?

For example, suppose I have something like this:

async void btnStartTask_Click(object sender, EventArgs e)
{
    await Task.Run(() => LongRunningTask());
}

bool LongRunningTask()
{
    // Establish some connection here.

    // Do some work here.

    List<Foo> incorrectValues = GetIncorrectValuesFromAbove();

    if (incorrectValues.Count > 0)
    {
        // Here, I want to present the "incorrect values" to the user (on the UI thread)
        // and let them select whether to modify a value, ignore it, or abort.
        var confirmedValues = WaitForUserInput(incorrectValues);
    }
    
    // Continue processing.
}

Is it possible to substitute WaitForUserInput() with something that runs on the UI thread, waits for the user's intervention, and then acts accordingly? If so, how? I'm not looking for complete code or anything; if someone could point me in the right direction, I would be grateful.

Servy

What you're looking for is almost exactly Progress<T>, except you want to have the thing that reports progress get a task back with some information that they can await and inspect the results of. Creating Progress<T> yourself isn't terribly hard., and you can reasonably easily adapt it so that it computes a result.

public interface IPrompt<TResult, TInput>
{
    Task<TResult> Prompt(TInput input);
}

public class Prompt<TResult, TInput> : IPrompt<TResult, TInput>
{
    private SynchronizationContext context;
    private Func<TInput, Task<TResult>> prompt;
    public Prompt(Func<TInput, Task<TResult>> prompt)
    {
        context = SynchronizationContext.Current ?? new SynchronizationContext();
        this.prompt += prompt;
    }

    Task<TResult> IPrompt<TResult, TInput>.Prompt(TInput input)
    {
        var tcs = new TaskCompletionSource<TResult>();
        context.Post(data => prompt((TInput)data)
            .ContinueWith(task =>
            {
                if (task.IsCanceled)
                    tcs.TrySetCanceled();
                if (task.IsFaulted)
                    tcs.TrySetException(task.Exception.InnerExceptions);
                else
                    tcs.TrySetResult(task.Result);
            }), input);
        return tcs.Task;
    }
}

Now you simply need to have an asynchronous method that accepts the data from the long running process and returns a task with whatever the user interface's response is.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pause execution and wait for user input

From Dev

Swift: How to let background thread wait for user input?

From Dev

How to Wait for Completion of ANY Worker Thread?

From Dev

How to kill a running thread in wait?

From Dev

Pause Main thread until background thread retrieves user input

From Dev

Qt - What is the correct way to pause execution of a program to wait for user input?

From Dev

How to pause running thread and restart same thread when needed?

From Dev

Thread ,UI thread, Worker Thread, Async Task

From Dev

Thread ,UI thread, Worker Thread, Async Task

From Dev

pause for user correction to input

From Dev

JavaScript Worker : how to check if a message was received while running expensive task

From Dev

PyQt4 Wait in thread for user input from GUI

From Dev

Pause Thread While Another Thread Is Executing A Task

From Dev

Pause Thread While Another Thread Is Executing A Task

From Dev

How to get Java to wait for user Input

From Dev

How to wait for thread to wait

From Dev

How to wait main thread until async task is completed in C#

From Dev

How to pause and resume thread?

From Dev

Thread / task wait for a button click

From Dev

TThread Wait for User Input

From Dev

Wait for user input

From Dev

how to send input from the MainThread to the worker thread in node js (Worker_threads)

From Dev

How to run a window into a new task and keep its main thread running

From Dev

How to stop the task running in a Thread Pool Executor Android

From Dev

How to run a window into a new task and keep its main thread running

From Dev

How to Update UI while a task is running in another thread in android?

From Dev

Let thread wait for dialog input

From Dev

Running small Operation on task/thread

From Dev

How to wait for a task to finish

Related Related

  1. 1

    Pause execution and wait for user input

  2. 2

    Swift: How to let background thread wait for user input?

  3. 3

    How to Wait for Completion of ANY Worker Thread?

  4. 4

    How to kill a running thread in wait?

  5. 5

    Pause Main thread until background thread retrieves user input

  6. 6

    Qt - What is the correct way to pause execution of a program to wait for user input?

  7. 7

    How to pause running thread and restart same thread when needed?

  8. 8

    Thread ,UI thread, Worker Thread, Async Task

  9. 9

    Thread ,UI thread, Worker Thread, Async Task

  10. 10

    pause for user correction to input

  11. 11

    JavaScript Worker : how to check if a message was received while running expensive task

  12. 12

    PyQt4 Wait in thread for user input from GUI

  13. 13

    Pause Thread While Another Thread Is Executing A Task

  14. 14

    Pause Thread While Another Thread Is Executing A Task

  15. 15

    How to get Java to wait for user Input

  16. 16

    How to wait for thread to wait

  17. 17

    How to wait main thread until async task is completed in C#

  18. 18

    How to pause and resume thread?

  19. 19

    Thread / task wait for a button click

  20. 20

    TThread Wait for User Input

  21. 21

    Wait for user input

  22. 22

    how to send input from the MainThread to the worker thread in node js (Worker_threads)

  23. 23

    How to run a window into a new task and keep its main thread running

  24. 24

    How to stop the task running in a Thread Pool Executor Android

  25. 25

    How to run a window into a new task and keep its main thread running

  26. 26

    How to Update UI while a task is running in another thread in android?

  27. 27

    Let thread wait for dialog input

  28. 28

    Running small Operation on task/thread

  29. 29

    How to wait for a task to finish

HotTag

Archive