跨线程事件在C#处理 [英] Cross-thread event handling in C#

查看:164
本文介绍了跨线程事件在C#处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与运行在单独的线程自己的事件调度程序的框架工作。该框架可能会产生一些事件



 类SomeDataSource {

公共事件OnFrameworkEvent;

无效FrameworkCallback(){

//这个函数在框架的线程上运行。

如果(OnFrameworkEvent!= NULL)
OnFrameworkEvent(参数);
}
}



我想这些事件交付给一个WinForms对象上的WinForms线程。我明明检查 InvokeRequired ,并根据需要将其分派到的WinForms线程。

 类SomeForm:表格{

// ...

公共无效SomeAction(SomeArgs参数){
如果(InvokeRequired){
的BeginInvoke(新的动作(SomeAction),参数);
的回报;
}

// ...
}

}

现在当窗体处于被关闭,这会导致各种问题的过程中的事件可以被传递,所以我注销从框架的事件源窗体的事件处理程序在这样的WinForms线程

  VAR形式=新SomeForm(); 
变种SRC =新SomeDataSource();

// ...

src.OnFrameworkEvent + = form.SomeAction;
form.Closing + =(发件人,eargs)=> src.OnFrameworkEvent - = form.SomeAction;




  1. 现在,是这种方法的线程安全?的如果窗体处于被关闭的过程中,与外国线程调用的BeginInvoke ,将调用仍在排队等待执行,如果窗体关闭? (这意味着我仍然有遇到相同问题的机会)


  2. 有没有跨线程事件处理更好的方法或建议的模式?



解决方案

没有事实并非如此。当你注销并关闭表的线程可能只是执行事件处理程序。小可能性,但不是零。你必须有线程停止,然后才能关闭窗体。如果你不想放弃它,你将必须继续通过取消的FormClosing事件公开的形式,然后让线程完成回调关闭表单。



检查这个线程获取更多信息。


I am working with a framework that runs its own event dispatcher in a separate thread. The framework may generate some events.

class SomeDataSource {

    public event OnFrameworkEvent;

    void FrameworkCallback() {

        // This function runs on framework's thread.

        if (OnFrameworkEvent != null)
            OnFrameworkEvent(args);
    }
}

I want to deliver these events to a Winforms object on Winforms thread. I obviously check for InvokeRequired and dispatch it to Winforms thread if necessary.

class SomeForm : Form {

    // ...

    public void SomeAction(SomeArgs args) {
        if (InvokeRequired) {
            BeginInvoke(new Action(SomeAction), args);
            return;
        }

        // ...
    }

}

Now events may be delivered when the form is in the process of being closed, which causes all sorts of problems, so I unregister the form's event handler from framework's event source on Winforms thread like this:

var form = new SomeForm();
var src = new SomeDataSource();

// ...

src.OnFrameworkEvent += form.SomeAction;
form.Closing += (sender, eargs) => src.OnFrameworkEvent -= form.SomeAction;

  1. Now, is this approach thread-safe? If the form is in the process of being closed, and a foreign thread calls BeginInvoke, will the invocation still be queued for execution if the form is closed? (which means I still have a chance of encountering the same problem)

  2. Is there a better approach or recommended pattern for cross-thread event handling?

解决方案

No it is not. The thread might just be executing the event handler while you unregister it and close the form. Small odds, but not zero. You have to have the thread stopped before you can close the form. If you don't want to abort it, you'll have to keep the form open by canceling the FormClosing event, then let the thread's completion callback close the form.

Check this thread for more info.

这篇关于跨线程事件在C#处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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