C# COM Cross Thread

旧城冷巷雨未停 提交于 2019-12-04 12:21:04

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.

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?

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).

Reference documentation about COM threading models:

Understanding and Using COM Threading Models
Processes, Threads and Appartments
Introduction to COM Interop

In the code you've posted, COM_Sim.client is never assigned. Is there something missing? It's hard to diagnose without seeing how the objects are being assigned.

Additionally: you currently have newMeasurement declared as a public field in COM_Sim, but newMeasurment (spelled differently) inside the method. As written, this code won't compile. It's possible that a similar typo exists in your actual method which is causing you to assign to a different object than the one you thought you created inside NewMeasurement.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!