调用BackgroundWorker来更新UI [英] Calling a BackgroundWorker to update the UI

查看:140
本文介绍了调用BackgroundWorker来更新UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi


我有一个Class,它包含一个BackgroundWorker,用于从数据库更新Form中的数据。另外我还有一个用于创建上下文菜单及其事件的类。

我必须从第二堂课的活动中调用backgroundworker。是否可以这样做。





示例代码



class Main

Hi
I have a Class which contains a BackgroundWorker to update the data in Form from database. Also I have a another class which is used to create a context menu and its events.
I have to call backgroundworker from the event in the second class. Is it Possible to do it.


sample Code

class Main

public partial class FormMain: Form
{
 private void bwMain_DoWork(object sender, DoWorkEventArgs e)
 {
   //Fetch data

 }
 private void bwMain_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
 {
   //Update UI  
 }
 
}

public class MenuMain
{
 MenuMain()
 {
  MenuItem miPayLoan = new MenuItem("Menu1", new EventHandler(Menu1_Click));
 }
  private void miPayLoan_Click(object sender, EventArgs e)
{
  //do some operations 
  //here call backgroundworker of above class to update Form   
}
}









谢谢..





Thanks ..

推荐答案

你好,



请试试这些链接:

http://stackoverflow.com/questions/10019960/update-gui-from-background-worker - 或者事件 [ ^ ]

http://stackoverflow.com/questions/1862590/how-to-update-gui-with-backgroundworker [ ^ ]
Hi there,

Please try these links:
http://stackoverflow.com/questions/10019960/update-gui-from-background-worker-or-event[^]
http://stackoverflow.com/questions/1862590/how-to-update-gui-with-backgroundworker[^]


当然。
private void miPayLoan_Click(object sender, EventArgs e)
{
    //do some operations
    //here call backgroundworker of above class to update Form
    MainForm.StartFormUpdatingMethod();
}

当然,您需要更多代码。在FormMain.cs中:

Of course, you need a bit more code. In your FormMain.cs:

public partial class FormMain : Form
{
    // This may also reside in FormMain.Designer.cs,
    //   depends on how you added it to the project.
    BackgroundWorker bwMain = new BackgroundWorker();

    public void StartFormUpdatingMethod()
    {
        bwMain.StartAsync();
    }

    // What you showed already
}

要使其工作,您需要引用 miPayLoan_Click()中提供的FormMain实例。如果它位于 FormMain 类本身内,那是微不足道的。否则你必须提供参考。

To make this work, you need a reference to the instance of FormMain available in miPayLoan_Click(). If it resides within the FormMain class itself, that's trivial. Otherwise you'll have to provide a reference.


这篇关于调用BackgroundWorker来更新UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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