通过多层体系结构中的委托进行线程之间的通信? [英] Communication between threads via delegates in Multi Tier Architecture?

查看:48
本文介绍了通过多层体系结构中的委托进行线程之间的通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找线程间通信的解决方案.我们有一个三层架构,

I am looking for a solution for Interthread communication. We got a Three Tier Architecture,

贵,参考逻辑参考设备控制器

Gui, references Logic references Devicecontroller

线程 A 是 Windows 应用程序的主线程.我启动了一个独立于线程 a 工作的线程 B,它们不共享代码.但是线程 A 必须得到一些关于线程 b 状态的反馈.我试着用一个代表来解决这个问题.我必须在 .net 3.5、c#、WEC7 上工作

Thread A is the main thread of a windows app. I starts a Thread B that is working independant of thread a, they do not share code. But thread A has to get some feedback about status of thread b. I try to solve this with a delegate. I have to work on .net 3.5, c#, WEC7

GUI 和 Logic 在线程 A 的上下文中运行DeviceController 运行在线程 B 的上下文中.两个线程都在长时间运行线程 A 启动并控制线程 B.线程 A(逻辑)从 B(设备控制器)获取信息并更新 Gui 或数据库

Gui and Logic run in context of Thread A DeviceController runs in Context of Thread B. Both Threads are long running Thread A starts and controls Thread B. Thread A (Logic) gets information back from B (DeviceController) and updates the Gui or the database

代码在线程A中执行很重要

It is important that the code is executed in Thread A

    public void OnMyEvent(string foo)
    {
        //there may be access to Gui here, there may be other actions, like accessing database
        // All this should be in context of thread A
        MessageBox.Show(foo);
    }

// The Gui
namespace WindowsFormsApplication1
{
    //This code is executed in Thread A, UIThread
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ThreadController threadController = new ThreadController();
            threadController.StartThread(this, e);
        }

    }

}

//Tier Logic, runs in Context of Thread A
namespace Logic
{
    //this class runs in the context of Thread A
    public  class ThreadController
    {
        public void StartThread(Object obj)
        {
            new ClassForSecondThread(obj as Parameters);
        }

        public void StartThread(object sender, EventArgs e)
        {
            //ParameterizedThreadStart threadstart = new ParameterizedThreadStart(startThread);
            ParameterizedThreadStart threadstart = new ParameterizedThreadStart(StartThread);
            Thread thread = new Thread(threadstart);
            Parameters parameters = new Parameters() {MyEventHandler = OnMyEvent};
            thread.Start(parameters);
        }

        public void OnMyEvent(string foo)
        {
            //there may be access to Gui here, there may be other actions, like accessing database
            // All this should be in context of thread A. Here it is unfortunately in Context Thread B
            MessageBox.Show(foo);
        }
    }
}

//在线程B的上下文中运行命名空间设备控制器{

//runs in context of Thread B namespace DeviceController {

//This class runs in the context of  Thread B
public class ClassForSecondThread
{
    public ClassForSecondThread(Parameters parameters)
    {
        if (parameters == null)
            return;
        MyEventhandler += parameters.MyEventHandler;
        DoWork();
    }

    private void DoWork()
    {
        //DoSomething
        if (MyEventhandler != null)
            MyEventhandler.DynamicInvoke("Hello World"); 
        Thread.Sleep(10000);
        if (MyEventhandler != null)
            MyEventhandler.DynamicInvoke("Hello World again");

    }

    private event MyEventHandler MyEventhandler;
}

public class Parameters
{
    public MyEventHandler MyEventHandler;
}

public delegate void MyEventHandler(string foo);

}

有两个问题,我还不能处理:

There are two issues, I cannot yet handle:

No 1:OnMyEvent 仍然在线程 b 的上下文中运行否 2:我需要以另一种方式进行相同的通信,如果 gui 中存在某些事件,则必须通知设备控制器,例如关机等

No 1: OnMyEvent still runs in context of thread b No 2: I need the same communication the other way, if there is some event in the gui, the devicecontroller has to be informed about e.g. shutdown etc.

推荐答案

如果线程 A 是 GUI 线程,您可以使用 Control.BeginInvoke 将委托调用分派给 UI 线程.

If the thread A is the GUI thread you could use Control.BeginInvoke to dispatch delegate calls to UI thread.

您只需要创建一个可以从线程 B 访问的 GUI 控件实例.然后只要该控件可见,您就可以对其调用 BeginInvoke.

You just have to make an instance of your GUI control to be accessible from your thread B. Then you can call BeginInvoke on it as long as that control is Visible.

要与设备控制器通信,您可能需要创建一个同步队列.

To communicate with device controller you will probably have to create a synchronized queue.

SynchronizationContext 类提供了基础为此,有一个实现可以从其他线程调度到 UI 线程.但要让它以相反的方式工作,您可能需要编写自己的实现.

SynchronizationContext class in .Net provide the basis for this and has an implementation that will work for dispatching from other thread to UI thread. But to make it work the other way around you may have to write your own implementation.

这篇关于通过多层体系结构中的委托进行线程之间的通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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