C#COM交叉线程 [英] C# COM Cross Thread

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

问题描述

我们正在开发一个软件来控制科学测量设备。它提供了一个COM接口定义几个函数来设置测量参数,并在测量数据时触发一个事件。

we're developing a software to control a scientific measuring device. it provides a COM-Interface defines several functions to set measurement parameters and fires an event when it measured data.

为了测试我们的软件,我实现了一个模拟的设备。

in order to test our software, I'm implementing a simulation of that device.

com对象运行一个循环,周期性地触发事件。客户端应用程序中的另一个循环现在应该使用给定的函数来设置com模拟器。

the com-object runs a loop which periodically fires the event. another loop in the client app should now setup up the com-simulator using the given functions.

我创建了一个测量参数的类,当设置一个新的测量。

I created a class for measuring parameters which will be instantiated when setting up a new measurement.

// COM-Object
public class MeasurementParams
{
    public double Param1;
    public double Param2;
}

public class COM_Sim : ICOMDevice
{
    public MeasurementParams newMeasurement;
    IClient client;

    public int NewMeasurement()
    {
        newMeasurment = new MeasurementParam();
    }

    public int SetParam1(double val)
    {
        // why is newMeasurement null when method is called from client loop
        newMeasurement.Param1 = val;
    }

    void loop()
    {
        while(true)
        {
            // fire event
            client.HandleEvent;
        }
    }
}

public class Client : IClient
{
    ICOMDevice server;

    public int HandleEvent()
    {
        // handle this event
        server.NewMeasurement();
        server.SetParam1(0.0);
    }

    void loop()
    {
        while(true)
        {
            // do some stuff...
            server.NewMeasurement();
            server.SetParam1(0.0);
        }
    }
}

独立线程。当调用server.NewMeasurement()时,服务器上的对象将设置为新实例。但在下一个函数中,对象再次为null。做同样的事情处理服务器事件,它工作完美,因为方法运行在服务器线程。如何使其从客户端线程工作。

both of the loops run in independent threads. when server.NewMeasurement() is called, the object on the server is set to a new instance. but in the next function, the object is null again. do the same when handling the server-event, it works perfectly, because the method runs in the servers thread. how to make it work from client-thread as well.

由于客户端意图使用真实设备,我不能修改制造商给出的接口。

As the client is meant to be working with the real device, I cannot modify the interfaces given by the manufacturer. also I need to setup measurements independent from the event-handler, which will be fired not regularly.

我假设这个问题与多线程COM行为相关,但是我没有找到任何东西这个主题。

I assume this problem related to multithreaded-COM behavior but I found nothing on this topic.

推荐答案

客户端和服务器的线程模型是什么 - STA或MTA? (参考 - STA是单线程Com对象,意味着允许一次仅从一个线程访问其公共方法,MTA是允许从多个线程对其公共方法进行concurent访问的多线程对象)

What is the threading model of the client and the server - STA or MTA? (For reference - STA are single-threaded Com objects meant to allow access to their public methods from only one thread at a time and MTA are multi-threaded objects that allow concurent access to their public methods from multiple threads)

你有多少个实例,你如何创建它们?我怀疑你只想要一个,但你结束了多个而不是。

How many instances do you have of each of them and how do you create these? I suspect you want only one, but you are ending up with multiple instead.

你有多少线程,哪个方法运行在哪个线程?如何创建这些线程,它们是否被初始化为运行STA对象或MTA?

How many threads do you have and which method is running on which thread? How do you create those threads and are they initialized to run STA objects or MTA?

注意: .NET足够聪明,客户端和服务器都被管理,并将COM从图片中取出。所以,你的模拟运行纯的托管代码。

Note: .NET is smart enough to detect if both the client and the server are managed and will take COM out of the picture. So, your simulation is running pure managed code. If you want proper test client, you need to write it as a C++ (as I suspect your device controller is unmanaged code).

有关COM线程模型的参考文档:

Reference documentation about COM threading models:

了解和使用COM线程模型

进程,主题和公寓

COM Interop简介

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

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