Архитектура Аудит Военная наука Иностранные языки Медицина Металлургия Метрология
Образование Политология Производство Психология Стандартизация Технологии


Using the Built-in Task-based Combinators



The System.Threading.Tasks namespace includes several key methods for working with and composing tasks.

Task.Run

The Task class exposes several Run methods that enable easily offloading work as a Task or Task<TResult> to the ThreadPool, e.g.

public async void button1_Click(object sender, EventArgs e)

{

textBox1.Text = await Task.Run(() =>

{

   // … do compute-bound work here

   return answer;

});

}

 

Some of these run methods (such as the Run<TResult>(Func<TResult>) overload used in the prior snippet) exist as shorthand for the TaskFactory.StartNew method that’s existed since .NET 4. Other overloads, however, such as Run<TResult>(Func<Task<TResult>>), enable await to be used within the offloaded work, e.g.

public async void button1_Click(object sender, EventArgs e)

{

pictureBox1.Image = await Task.Run(() =>

{

   using(Bitmap bmp1 = await DownloadFirstImageAsync())

   using(Bitmap bmp2 = await DownloadSecondImageAsync())

   return Mashup(bmp1, bmp2);

});

}

 

Such overloads are logically equivalent to using StartNew in conjunction with the Unwrap extension method in the Task Parallel Library.

Task.FromResult

For scenarios where data may already be available and simply needs to be returned from a task-returning method lifted into a Task<TResult>, the Task.FromResult method may be used:

public Task<int> GetValueAsync(string key)

{

int cachedValue;

return TryGetCachedValue(out cachedValue) ?

   Task.FromResult(cachedValue) :

   GetValueAsyncInternal();

}

 

private async Task<int> GetValueAsyncInternal(string key)

{

}

 

Task.WhenAll

The WhenAll method is used to asynchronously wait on multiple asynchronous operations represented as Tasks. It has multiple overloads in order to accommodate a set of non-generic tasks or a non-uniform set of generic tasks (e.g. asynchronously waiting for multiple void-returning operations, or asynchronously waiting for multiple value-returning methods where each of the values may be of a different type) as well as a uniform set of generic tasks (e.g. asynchronously waiting for multiple TResult-returning methods).

Consider the need to send emails to several customers. We can overlap the sending of all of the emails (there’s no need to wait for one email to complete sending before sending the next), and we need to know when the sends have completed and if any errors occurred:

IEnumerable<Task> asyncOps = from addr in addrs select SendMailAsync(addr);

await Task.WhenAll(asyncOps);

 

The above code does not explicitly handle exceptions that may occur, instead choosing to let exceptions propagate out of the await on WhenAll’s resulting task. To handle the exceptions, the developer could employ code like the following:

IEnumerable<Task> asyncOps = from addr in addrs select SendMailAsync(addr);

try

{

await Task.WhenAll(asyncOps);

}

catch(Exception exc)

{

...

}

 

In the previous case, if any of the asynchronous operations failed, all of the exceptions will be gathered up into an AggregateException which will be stored in the Task returned from WhenAll. However, only one of those exceptions will be propagated by the await keyword. If it is important to be able to examine all of the exceptions, the above snippet may be rewritten as follows:

Task [] asyncOps = (from addr in addrs select SendMailAsync(addr)).ToArray();

try

{

await Task.WhenAll(asyncOps);

}

catch(Exception exc)

{

foreach(Task faulted in asyncOps.Where(t => t.IsFaulted))

{

   … // work with faulted and faulted.Exception

}

}

 

Consider another example of downloading multiple files from the web asynchronously. In this case, all of the asynchronous operations have homogeneous result types, and access to the results is simple:

string [] pages = await Task.WhenAll(

from url in urls select DownloadStringAsync(url));

 

As in the previous void-returning case, the same exception handling techniques are usable here:

Task [] asyncOps =

(from url in urls select DownloadStringAsync(url)).ToArray();

try

{

string [] pages = await Task.WhenAll(asyncOps);

...

}

catch(Exception exc)

{

foreach(Task<string> faulted in asyncOps.Where(t => t.IsFaulted))

{

   … // work with faulted and faulted.Exception

}

}

Task.WhenAny

The WhenAny API is used to asynchronously wait on multiple asynchronous operations represented as Tasks, asynchronously waiting for just one of them to complete. There are four primary uses for WhenAny:

· Redundancy. Doing an operation multiple times and selecting the one that completes first (e.g. contacting multiple stock quote Web services that will all produce a single result and selecting the one that completes the fastest).

· Interleaving. Launching multiple operations and needing them all to complete, but processing them as they complete.

· Throttling. Allowing additional operations to begin as others complete. This is an extension of the interleaving case.

· Early bailout. An operation represented by t1 can be grouped in a WhenAny with another task t2, and we can wait on the WhenAny task. t2 could represent a timeout, or cancellation, or some other signal that will cause the WhenAny task to complete prior to t1 completing.

Redundancy

Consider a case where we want to make a decision about whether to buy a stock. We have several stock recommendation Web services that we trust, but based on daily load each of the services can end up being fairly slow at different times. We can take advantage of WhenAny to be made aware when any of the operations completes:

var recommendations = new List<Task<bool>>()

{

 GetBuyRecommendation1Async(symbol),

GetBuyRecommendation2Async(symbol),

GetBuyRecommendation3Async(symbol)

};

Task<bool> recommendation = await Task.WhenAny(recommendations);

if (await recommendation) BuyStock(symbol);

 

Unlike WhenAll, which in the case of successful completion of all tasks returns a list of their unwrapped results, WhenAny returns the Task that completed: if a task fails, it’s important to be able to know which failed, and if a task succeeds, it’s important to be able to know with which task the returned value is associated. Given this, we need to access the returned task’s Result property, or further await it as is done in this example.

As with WhenAll, we need to be able to accommodate exceptions. Due to having received back the completed task, we can await the returned task in order to have errors propagated, and try/catch them appropriately, e.g.

Task<bool> [] recommendations = …;

while(recommendations.Count > 0)

{

Task<bool> recommendation = await Task.WhenAny(recommendations);   

try

{

   if (await recommendation) BuyStock(symbol);

   break;

}

catch(WebException exc)

{

   recommendations.Remove(recommendation);

}

}       

 

Additionally, even if a first task completes successfully, subsequent tasks may fail. At this point, we have several options in how we deal with their exceptions. One use case may dictate that we not make further forward progress until all of the launched tasks have completed, in which case we may utilize WhenAll. A second use case dictates that all exceptions are important and must be logged.  For this, we can utilize continuations directly to receive a notification when tasks have asynchronously completed:

foreach(Task recommendation in recommendations)

{

var ignored = recommendation.ContinueWith(

   t => { if (t.IsFaulted) Log(t.Exception); });

}

 

or

foreach(Task recommendation in recommendations)

{

var ignored = recommendation.ContinueWith(

   t => Log(t.Exception), TaskContinuationOptions.OnlyOnFaulted);

}

 

or even:

private static async void LogCompletionIfFailed(IEnumerable<Task> tasks)

{

foreach(var task in tasks)

{

   try { await task; }

   catch(Exception exc) { Log(exc); }

}
}

LogCompletionIfFailed(recommendations);

 

Finally, the developer may actually want to cancel all of the remaining operations.

var cts = new CancellationTokenSource();

var recommendations = new List<Task<bool>>()

{

GetBuyRecommendation1Async(symbol, cts.Token),

GetBuyRecommendation2Async(symbol, cts.Token),

GetBuyRecommendation3Async(symbol, cts.Token)

};

 

Task<bool> recommendation = await Task.WhenAny(recommendations);

cts.Cancel();

if (await recommendation) BuyStock(symbol);


Interleaving

Consider a case where we’re downloading images from the Web and doing some processing on each image, such as adding it to a UI control. We need to do the processing sequentially (on the UI thread in the case of the UI control example) but we want to download with as much concurrency as possible, and we don’t want to hold up adding the images to the UI until they’re all downloaded, but rather add them as they complete:

List<Task<Bitmap>> imageTasks =

(from imageUrl in urls select GetBitmapAsync(imageUrl)).ToList();

while(imageTasks.Count > 0)

{

try

{

   Task<Bitmap> imageTask = await Task.WhenAny(imageTasks);

   imageTasks.Remove(imageTask);

 

   Bitmap image = await imageTask;

   panel.AddImage(image);

}

catch{}

}

 

That same interleaving could be applied to a scenario involving not only downloads but also computationally-intensive processing on the ThreadPool of the downloaded images, e.g.

List<Task<Bitmap>> imageTasks =

(from imageUrl in urls select GetBitmapAsync(imageUrl)

    .ContinueWith(t => ConvertImage(t.Result)).ToList();

while(imageTasks.Count > 0)

{

try

{

   Task<Bitmap> imageTask = await Task.WhenAny(imageTasks);

   imageTasks.Remove(imageTask);

 

   Bitmap image = await imageTask;

   panel.AddImage(image);

}

catch{}

}

Throttling

Consider the same case as in the Interleaving example, except the user is downloading so many images that the downloads need to be explicitly throttled, e.g. only 15 downloads may happen concurrently. To achieve this, a subset of the asynchronous operations may be invoked. As operations complete, additional ones may be invoked to take their place.

const int CONCURRENCY_LEVEL = 15;

Uri [] urls = …;

int nextIndex = 0;

var imageTasks = new List<Task<Bitmap>>();

while(nextIndex < CONCURRENCY_LEVEL && nextIndex < urls.Length)

{

imageTasks.Add(GetBitmapAsync(urls[nextIndex]));

nextIndex++;

}

 

while(imageTasks.Count > 0)

{

try

{

   Task<Bitmap> imageTask = await Task.WhenAny(imageTasks);

   imageTasks.Remove(imageTask);

 

   Bitmap image = await imageTask;

   panel.AddImage(image);

}

catch(Exception exc) { Log(exc); }

 

if (nextIndex < urls.Length)

{

   imageTasks.Add(GetBitmapAsync(urls[nextIndex]));

   nextIndex++;

}

}

 

Early Bailout

Consider asynchronously waiting for an operation to complete while simultaneously being responsive to a user’s cancellation request (e.g. by clicking a cancel button in the UI).

private CancellationTokenSource m_cts;

 

public void btnCancel_Click(object sender, EventArgs e)

{

if (m_cts != null) m_cts.Cancel();

}

 

public async void btnRun_Click(object sender, EventArgs e)

{

m_cts = new CancellationTokenSource();

btnRun.Enabled = false;

try

{

    Task<Bitmap> imageDownload = GetBitmapAsync(txtUrl.Text);

     await UntilCompletionOrCancellation(imageDownload, m_cts.Token);

    if (imageDownload.IsCompleted)

    {

       Bitmap image = await imageDownload;

       panel.AddImage(image);

     }

    else imageDownload.ContinueWith(t => Log(t));

}

finally { btnRun.Enabled = true; }

}

 

private static async Task UntilCompletionOrCancellation(

Task asyncOp, CancellationToken ct)

{

var tcs = new TaskCompletionSource<bool>();

using(ct.Register(() => tcs.TrySetResult(true)))

    await Task.WhenAny(asyncOp, tcs.Task);

return asyncOp;

}

 

This implementation reenables the user interface as soon as we decide to bail out but without canceling the underlying asynchronous operations. Another alternative would be to cancel the pending operations when we decide to bail out, however not reestablish the user interface until the operations actually complete, potentially due to ending early due to the cancellation request:

private CancellationTokenSource m_cts;

 

public async void btnRun_Click(object sender, EventArgs e)

{

m_cts = new CancellationTokenSource();

 

btnRun.Enabled = false;

try

{

    Task<Bitmap> imageDownload = GetBitmapAsync(txtUrl.Text, m_cts.Token);

    await UntilCompletionOrCancellation(imageDownload, m_cts.Token);

   Bitmap image = await imageDownload;

   panel.AddImage(image);

}

catch(OperationCanceledException) {}

finally { btnRun.Enabled = true; }

}

 

Another example of using WhenAny for early bailout involves using Task.WhenAny in conjunction with Task.Delay.

Task.Delay

As shown earlier, the Task.Delay method may be used to introduce pauses into an asynchronous method’s execution. This is useful for all kinds of functionality, including building polling loops, delaying the handling of user input for a predetermined period of time, and the like. It can also be useful in combination with Task.WhenAny for implementing timeouts on awaits.

If a task that’s part of a larger asynchronous operation (e.g. an ASP.NET Web service) takes too long to complete, the overall operation could suffer, especially if the operation fails to ever complete. Towards this end, it’s important to be able to timeout waiting on an asynchronous operation. The synchronous Task.Wait, WaitAll, and WaitAny methods accept timeout values, but the corresponding ContinueWhenAll/Any and the aforementioned WhenAll/WhenAny APIs do not. Instead, Task.Delay and Task.WhenAny may be used in combination to implement a timeout.

Consider a UI application which wants to download an image and disable the UI while the image is downloading. If the download takes too long, however, the UI should be re-enabled and the download should be discarded.

public async void btnDownload_Click(object sender, EventArgs e)

{

btnDownload.Enabled = false;

try

{

    Task<Bitmap> download = GetBitmapAsync(url);

    if (download == await Task.WhenAny(download, Task.Delay(3000)))

   {

       Bitmap bmp = await download;

       pictureBox.Image = bmp;

       status.Text = “Downloaded”;

    }

    else

    {

       pictureBox.Image = null;

       status.Text = “Timed out”;

       var ignored = download.ContinueWith(

           t => Trace(“Task finally completed”));

    }

}

finally { btnDownload.Enabled = true; }

}

 

The same applies to multiple downloads, since WhenAll returns a task:

public async void btnDownload_Click(object sender, RoutedEventArgs e)

{

btnDownload.Enabled = false;

try

{

    Task<Bitmap[]> downloads =

        Task.WhenAll(from url in urls select GetBitmapAsync(url));

     if (downloads == await Task.WhenAny(downloads, Task.Delay(3000)))

     {

      foreach(var bmp in downloads) panel.AddImage(bmp);

       status.Text = “Downloaded”;

     }

      else

    {

        status.Text = “Timed out”;

       downloads.ContinueWith(t => Log(t));

    }

}

finally { btnDownload.Enabled = true; }

}


Поделиться:



Последнее изменение этой страницы: 2019-06-09; Просмотров: 217; Нарушение авторского права страницы


lektsia.com 2007 - 2024 год. Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав! (0.099 с.)
Главная | Случайная страница | Обратная связь