I've written a C# program that talks to a COM server to conduct simulations. It works without any trouble, but the simulations being carried out by the COM server are fairly processor intensive, and only run single core.
As such, I've used Parallel.For to distribute the workload amongst multiple threads. It appears, however, that all the simulation results generated by the COM server are shared amongst all instances of its classes, so when I'm running the parallel task with only 1 thread, everything works as expected, but when I'm running the task with multiple threads, the results are completely garbled (as multiple threads are effectively causing the simulation engine to replace its results with new ones as they are being read).
I was wondering if there was a way to connect to the COM server multiple times in order to stop the results-sharing of class instances?
Edit
My process for connecting to the COM server was to:
- Add a reference using Project->Add References->COM (VS2010)
Use the following code to instantiate the simulator object:
dss = new OpenDSSengine.DSS(); dss.Start(0);
The above code is called in the local thread data initialiser (localInit) parameter of Parallel.For, and thus a new dss
object is created for each thread, but the results obtained seem to be common across all threads.
The COM server is a dll.