返回进度状态

时间:2014-08-31 09:38:56

标签: c#

如何从下面返回进度状态?我是否正确使用IProgress状态?我曾经假设某个地方会有一个Progress.report()函数,但它似乎并不存在。

        public static void Main()
    {
        Console.WriteLine("Running Async Methods");
        AsynchronousMethods am = new AsynchronousMethods();

        Progress<int> p = new Progress<int>();
        Task ta = am.asyncMethods(p);


        while (!ta.IsCompleted)
        {
            // report progress
            // I would liek to return the Progress here. I am not sure how? 
            // there doesnt seem to be a P.result
        }
        ta.Wait();
    }


    public async Task asyncMethods(IProgress<int> pro)
    {
        await Task.Run(() =>
        {
            for (int i = 0; i < 1999; i++)
            {
                Console.WriteLine("I is {0}", i);
                pro.Report(i * 2);
            }
        });
    }

1 个答案:

答案 0 :(得分:2)

您应该处理ProgressChanged事件

Progress<int> p = new Progress<int>();
p.ProgressChanged += p_ProgressChanged;


static void p_ProgressChanged(object sender, int e)
{
    Console.WriteLine("Progress :"+e);
}