C# COM Cross Thread problem

Posted by user364676 on Stack Overflow See other posts from Stack Overflow or by user364676
Published on 2010-06-11T15:50:10Z Indexed on 2010/06/11 15:52 UTC
Read the original article Hit count: 360

Filed under:
|
|
|
|

Hi,

we're developing a software to control a scientific measuring device. it provides a COM-Interface defines serveral 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.

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 instanciated 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);
        }
    }
}

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 manufactor. also i need to setup measurements independent from the event-handler, which will be fired not regulary.

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about multithreading