在具有MVP模式的Winforms(C#)中使用backgroundworker [英] using backgroundworker in Winforms (C#) with MVP pattern

查看:68
本文介绍了在具有MVP模式的Winforms(C#)中使用backgroundworker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试通过使用MVP模式来重构应用程序的意大利面条代码.但是现在我为此感到挣扎:

I've been trying to refactor a spaghetti code of an app by using MVP pattern. But now I'm struggling with this:

具有一个按钮的窗体,该按钮调用Dowork方法(后台工作人员),这是一个很长的操作.我的问题是,如果我将长时间操作从视图中移出到Presenter中,那么如何从该操作向视图发送进度更改? BGW也必须在Presenter中吗? 您能给我一个如何做的例子吗?

A form that has button that calls a the DoWork method (of a backgroundworker) which is a long operation. My question is if I move the long operation out of the view into the Presenter then how do I send progress changes from this operation to the View? The BGW must be in the Presenter also? Can you give me a sample of how to do this?

谢谢.

推荐答案

这概述了BackgroundWorker的使用:

This outlines the use of the BackgroundWorker:

private BackgroundWorker _backgroundWorker;

public void Setup( )
{
    _backgroundWorker = new BackgroundWorker();
    _backgroundWorker.WorkerReportsProgress = true;
    _backgroundWorker.DoWork +=
      new DoWorkEventHandler(BackgroundWorker_DoWork);
    _backgroundWorker.ProgressChanged +=
      new ProgressChangedEventHandler(BackgroundWorker_ProgressChanged);
    _backgroundWorker.RunWorkerCompleted +=
      new RunWorkerCompletedEventHandler(BackgroundWorker_RunWorkerCompleted);

    // Start the BackgroundWorker
    _backgroundWorker.RunWorkerAsync();
}

void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    // This method runs in a background thread. Do not access the UI here!
    while (work not done) {
        // Do your background work here!

        // Send messages to the UI:
        _backgroundWorker.ReportProgress(percentage_done, user_state);
        // You don't need to calculate the percentage number if you don't
        // need it in BackgroundWorker_ProgressChanged.
    }
    // You can set e.Result = to some result;
}

void BackgroundWorker_ProgressChanged(object sender,
                                      ProgressChangedEventArgs e)
{
    // This method runs in the UI thread and receives messages from the backgroud thread.

    // Report progress using the value e.ProgressPercentage and e.UserState
}

void BackgroundWorker_RunWorkerCompleted(object sender,
                                         RunWorkerCompletedEventArgs e)
{
    // This method runs in the UI thread.
    // Work is finished! You can display the work done by using e.Result
}


更新

此BackgroundWorker必须在cause的演示者中. MVP,MVC或MVVM之类的模式的想法是从视图中删除尽可能多的代码.该视图将仅具有非常特定于该视图本身的代码,例如在Paint事件处理程序中创建视图或绘图等.视图中的另一种代码是与演示者或控制器进行通信所必需的代码.但是,演示逻辑必须在演示者中.

This BackgroundWorker has to be in the presenter of cause. The idea of patterns like MVP, MVC or MVVM is to remove as much code from the view as possible. The view would only have code very specific to the view itself, like creating the view or drawing in the Paint event handler and so on. Another kind of code in the view is the code necessary to communicate with the presenter or controller. The presenting logic, however, has to be in the presenter.

您将使用在UI线程中运行的BackgroundWorker_ProgressChanged方法将更改发送到视图.通过调用视图的公共方法,设置视图的公共属性或公开视图的公共属性,可以通过将视图的属性或控件的属性绑定到视图来附加视图. (这是从MVVM模式中借用的.)如果您决定将视图绑定到演示者的属性,则演示者必须实现INotifyPropertyChanged以便通知视图属性已更改.

You would use the BackgroundWorker_ProgressChanged method that runs in the UI thread to send changes to the view. Either by calling public methods of the view or by setting public properties of the view or by exposing public properties the view can attach to by binding its properties or the properties of its controls to it. (This is borrowed from the MVVM pattern.) The presenter must implement INotifyPropertyChanged in order to notify the view that a property has changed, if you decide to bind the view to properties of the presenter.

注意:不允许UI线程以外的其他线程直接与视图交互(如果尝试这样做,则会引发异常).因此,BackgroundWorker_DoWork无法直接与视图交互,因此会调用ReportProgress,后者又会在UI线程中运行BackgroundWorker_ProgressChanged.

Note: Another thread than the UI thread is not allowed to interact with the view directly (an exception is thrown if you try to do so). Therefore the BackgroundWorker_DoWork cannot interact with the view directly and therefore calls ReportProgress, which in turn runs BackgroundWorker_ProgressChanged in the UI thread.

这篇关于在具有MVP模式的Winforms(C#)中使用backgroundworker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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