C#COM跨线程 [英] C# COM Cross Thread

查看:181
本文介绍了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()被调用时,在服务器上的对象被设置为一个新的实例。但在接下来的功能,对象为空一次。做同样的处理时,在服务器的事件,它完美的作品,因为该方法在服务器线程中运行。如何使它从客户线程正常工作。

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都意味着允许的时间和MTA从只有一个线程访问他们的公共方法单线程COM对象的多线程对象允许从多个线程的公共方法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出来的画面。所以,你的模拟运行的纯托管代码。如果你想适当的测试客户端,你需要把它写成一个C ++(我怀疑你的设备控制器非托管代码)

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线程模型结果
进程,线程和Appartments公寓结果
介绍COM互操作

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

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