Asynchronous Programming

Batista

Executive Member
Joined
Sep 2, 2011
Messages
7,880
#21
Thanks, I see that it updates at the end but just wanted to know
how do I know I'm back in the Sync part again?
Or asked otherwise, the alldone part is that in the Worker_ProgressChange portion
where e.ProgressPercentage = 100

Just want to know where I can start changing the UI parts again.
It seems anytime I touched any control the system just stalled, so had to rewrite but luckily not a lot.
Generate a backgroundworker_finished event,not sure of the name maybe its 'done', and that notifies your main thread that its done.
 

XennoX

Expert Member
Joined
Nov 15, 2007
Messages
2,204
#22
Either: explicit use of the TPL as @[)roi(] mentioned, or implement the async/await pattern.

C#:
class Program
{
    static async Task Main(string[] args)
    {
        // do some preparatory work
        // ..
        // ..
        CancellationTokenSource cts = new CancellationTokenSource();
        var workTask = DoWorkAsync(10, cts.Token);

        // I can do some other work here, whilst I wait for workTask to finish.
        // However, if I need to have the result before I can proceed then I need to use the `await` keyword
        // The method will pause at this point, and return to the calling code.
        // When the task completes, it will return here and carry on.
        // If the Task becomes faulted, an exception will be thrown.
        var workResult = await workTask;

        Console.WriteLine(workResult.HasValue ? workResult.ToString() : "No value");

        // Some useful properties
        // faulted property: workTask.IsFaulted
        // completed property: workTask.IsCompleted
        // Task result: workTask.Result (this assumes the return type is `Task<TResult>`, as `Task` is the async version of `void`**.
    }

    static async Task<int?> DoWorkAsync(int delayBy, CancellationToken cancellationToken)
    {
        if (cancellationToken.IsCancellationRequested)
            return null;

        await Task.Delay(TimeSpan.FromSeconds(delayBy), cancellationToken);

        return 42;
    }
}
Technically the compiler will allow your to write something like this:
C#:
async void MethodName()
{
   
}
Don't do this, as you cannot catch any exceptions, you cannot monitor the status of the task.
 
Last edited:

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
5,826
#23
Either: explicit use of the TPL as @[)roi(] mentioned, or implement the async/await pattern.
That's not really quite comparable, because many async/await functions are not typical designed for parallel computes; whilst you can fire a lot of them off with e.g. Task.Run a lot of effort will preceding this be required to manually split the workload and marshal the separate completions -- the degree of parallelism and concurrency isn't as clean as something like this:

C#:
var result = from s in (from x in xs.AsParallel()
                        from y in ys.AsParallel()
                        from z in zs.AsParallel()
                        select sum(x, y, z).AsParallel())
select foobar(s).AsParallel();
Task is really just a Promise and one would typically wrap a parallel compute in either a Promise or Future to run concurrent with the main thread and to finally marshal it back to main thread.
 
Last edited:
Top