Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Support of progress reporting and incremental results in .NET 4.0 "Task Parallel Library"

I know that Task Parallel Library is still in Beta and there are likely to be less resources available but from whatever I have read, library gives very special treatment to task scheduling, exception handling and cancellation.

But I don't find any references to progress reporting and sending incremental results from tasks. These 2 things seem too important to ignore. Can you throw some light on how to handle these in Task Parallel Library or refer some articles which explains them?

like image 537
Hemant Avatar asked Oct 08 '09 05:10

Hemant


1 Answers

This example updates a progress bar:

using System;   
using System.Threading;   
using System.Threading.Tasks;   
using System.Windows.Forms;   

class SimpleProgressBar : Form   
{   
    [STAThread]   
    static void Main(string[] args)   
    {   
        Application.EnableVisualStyles();   
        Application.Run(new SimpleProgressBar());   
    }   

    protected override void OnLoad(EventArgs e)   
    {   
        base.OnLoad(e);   

        int iterations = 100;   

        ProgressBar pb = new ProgressBar();   
        pb.Maximum = iterations;   
        pb.Dock = DockStyle.Fill;   
        Controls.Add(pb);   

        Task.ContinueWith(delegate   
        {   
            Parallel.For(0, iterations, i =>  
            {   
                Thread.SpinWait(50000000); // do work here   
                BeginInvoke((Action)delegate { pb.Value++; });   
            });   
        });   
    }   
}  

Updating a progress bar from inside a Parallel.For

like image 70
Robert Harvey Avatar answered Oct 14 '22 00:10

Robert Harvey