在单独的线程上启动事件处理程序类时,GUI更新吗? [英] GUI update when starting event handler class on separate thread?

查看:65
本文介绍了在单独的线程上启动事件处理程序类时,GUI更新吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个DLL,用于监视状态变化并查找来自单独购买的产品的事件.实际上,这是西门子公司的ScreenPop API,适用于可能知道这是什么的那些人.我正在使用C#.NET 3.5作为平台.

We have a DLL that monitors changes in status and looks for events from a separate, purchased product. Actually, it is a ScreenPop API from Siemens, for those of you who may know what that is. I am using C#.NET 3.5 as the platform.

此API需要很短的时间进行初始化,因此我们想使用一个单独的线程对其进行初始化.当前,我们在名为ScreenPop的类中具有此功能.该类监视2个事件,一个状态更改事件和一个屏幕弹出事件(告诉我们谁在呼叫客户的数据).

This API takes a looong time to initialize, so we want to use a separate thread to initialize it. Currently, we have the functionality in a class called ScreenPop. The class monitors 2 events, a status change event and a screen pop event (data that tells us who the customer is that is calling).

当前实现的方式无效,或者至少不能可靠地运作.在ScreenPop类中,有一个初始化方法,其中放置了所有长时间运行的启动代码.这是从类的构造函数中调用的,如下所示:

The way this is currently implemented doesn't work, or at least doesn't work reliably. Within the ScreenPop class, there is an initialization method where all the long-running startup code is placed. This is called from the constructor of the class, like this:

public ScreenPop( string Address, int Ext, CallbackStatusType pStatusFunc,
      CallbackScreenPopType pPopFunc 
)
{
    CallbackStatus = pStatusFunc;
    CallbackPopup = pPopupFunc;

    Thread t = new Thread( StartInBackground );
    t.Start();
}

在GUI代码中,pStatusFunc处的func更新状态标签,而pPopupFunc处的func将触发其他代码以进行屏幕弹出-现在,它仅显示事件中的数据.

In the GUI code, the func at pStatusFunc updates a status label, and the func at pPopupFunc will fire off some other code to do the screen pop - right now it just displays the data from the event.

缺少很多胶水,但我希望你明白这一点.这种方法的问题是未更新GUI.我知道事件会触发事件并运行事件处理程序,并且会调用回调函数,并且它们似乎应该在运行,但是GUI从未更新.

There is a lot of glue missing, but I hope you get the point. The problem with this approach is the GUI is not updated. I know the events fire and the event handlers run, and the callback functions are getting called and they seem like they should be running, but the GUI is never updated.

所以,我的问题是,我应该放弃它,转而使用BackgroundWorker方法吗?还是我只是缺少一些要更新GUI的东西?

So, my question is, should I abandon this in favor of a BackgroundWorker approach? Or am I just missing something in getting the GUI to update?

更多信息请咨询... 谢谢, 戴夫

More info on request... Thanks, Dave

推荐答案

您永远无法从其他线程(仅从启动应用程序的UI线程)更新GUI.您需要使用Control.Invoke方法在UI线程上运行代码.表单实例frmMain.Invoke.

You can never update the GUI from a different thread - only from the UI thread, which is the one that started the application. You need to use the Control.Invoke method to run code on the UI thread. Form instance, frmMain.Invoke.

这篇关于在单独的线程上启动事件处理程序类时,GUI更新吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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