如何提供反馈UI的异步方法? [英] How to provide a feedback to UI in a async method?

查看:221
本文介绍了如何提供反馈UI的异步方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发Windows窗体的项目中,我有10个任务做了,我想做到这一点的异步办法。 这些任务将出演时,在一个按钮,用户点击和我称之为异步的方法来做到这一点。在我的code,我已经对这些过程的参数列表。

我的问题是:

  

a)如何改变我的code并行运行的所有进程? (我想实现异步/待机)

     

B)如何提供反馈到我的UI应用程序?

在code波纹管是我曾尝试:

我的按钮来调用一个方法来启动进程

 私人无效的button1_Click(对象发件人,EventArgs的)
{
    //近15的过程
    的foreach(在进程VAR方法)
    {
        //调用一个异步方法来处理
        ProcessObject(过程);
    }
}
 

的方法来模拟我的过程中得到一个参数

 专用异步无效ProcessObject(ProcessViewModel过程)
{
    //这是我的循环范围,我需要并行运行
    {
       //我的code是这里

       //增加这个过程的进度
       process.Progress ++;

       //反馈UI(访问UI控件)
       UpdateRow(过程);
    }
}
 

我想这一点,但我不知道这是否是正确的方式来更新我的UI(网格)。

 私人无效UpdateRow(ProcessViewModel过程)
{
    dataGridView1.Rows [process.Index  -  1] .Cells [1]。价值= process.Progress;
    dataGridView1.Refresh();
}
 

解决方案

首先,说异步无效方法(事件处理程序有例外),因为里面抛出的异常也不会被察觉。相反,使用异步任务并等待吧。

 专用异步无效的button1_Click(对象发件人,EventArgs五)//<  - 注意异步修改
{
    //近15的过程
    的foreach(在进程VAR方法)
    {
        //调用一个异步方法来处理
        等待ProcessObject(过程);
    }
}

私人异步任务ProcessObject(ProcessViewModel过程)//<  - 需要注意的返回类型
{
    //我的code是这里的一些循环
    等待Task.Run(()=>
    {
        //将线程池的线程中运行
        //做任何CPU密集型的工作在这里
    });

    //此时code运行在UI线程
    process.Progress ++;

    //反馈给用户界面
    UpdateRow(过程);
}
 

然而,这将在一个时间开始只有一个任务,一旦做到这一点,将开始下一。如果你想开始所有的人立刻就可以开始他们并使用 Task.WhenAll ,期待着。

 专用异步无效的button1_Click(对象发件人,EventArgs五)//<  - 注意异步修改
{
     变种任务= Processes.Select(ProcessObject).ToList();
     等待Task.WhenAll(任务);
}
 

I have been developing a windows forms project where I have a 10 tasks to do, and I would like to do this in a async way. These tasks will star when the user click in a button and I call a async method to do this. In my code, I already have a list of parameters for these processes.

My questions is:

A) How transform my code to run all process in parallel? (I would like to implement async/await)

B) How to provide a feedback to my UI application?

The code bellow is what I have tried:

My button to call a method to start processes

private void button1_Click(object sender, EventArgs e)
{
    // almost 15 process
    foreach (var process in Processes)
    {
        // call a async method to process
        ProcessObject(process);
    }
}

The method to simulate my process getting a parameter

private async void ProcessObject(ProcessViewModel process)
{
    // this is my loop scope, which I need to run in parallel
    {
       // my code is here

       // increment the progress of this process
       process.Progress++;

       // feedback to UI (accessing the UI controls)
       UpdateRow(process);
    }
}

I tried this, but I am not sure if it is the right way to update my UI (a grid).

private void UpdateRow(ProcessViewModel process)
{
    dataGridView1.Rows[process.Index - 1].Cells[1].Value = process.Progress;
    dataGridView1.Refresh();
}

解决方案

First of all, say no to async void methods (Event handlers are exceptions) because exceptions thrown inside it will not be noticed. Instead use async Task and await it.

private async void button1_Click(object sender, EventArgs e)// <--Note the async modifier
{
    // almost 15 process
    foreach (var process in Processes)
    {
        // call a async method to process
        await ProcessObject(process);
    }
}

private async Task ProcessObject(ProcessViewModel process)// <--Note the return type
{
    // my code is here with some loops
    await Task.Run(()=>
    {
        //Will be run in ThreadPool thread
        //Do whatever cpu bound work here
    });

    //At this point code runs in UI thread
    process.Progress++;

    // feedback to UI
    UpdateRow(process);
}

However, this will start only one task at a time, once that is done it will start next. If you want to start all of them at once you can start them and use Task.WhenAll to await it.

private async void button1_Click(object sender, EventArgs e)// <--Note the async modifier
{
     var tasks = Processes.Select(ProcessObject).ToList();
     await Task.WhenAll(tasks);
}

这篇关于如何提供反馈UI的异步方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆