Hey all.
I've got a legacy COM dll that I'd like to use in my multithreaded C#
app. Problem though is when I create several instances of the interop
object and start them on their own threads they always seem to run
synchronously.
I've boiled down the code to a simple "lab" experiment below. In this
console app you'll see that instance one comes back after roughly 4
seconds and then instance two comes back in 8 seconds, twice the time.
This indicates that it is wait on the first thread to finish. Why
won't they run concurrently?
I've tried my best to make sure that the thread types are MTAThread
before calling the COM objects. Also, in VB I made sure the project
threading model was "Apartment Threaded" and the Instancing set to
GlobalUseMulti.
Any ideas???
Thanks
*************** **** .NET C# CONSOLE TEST APP *************** *****
using System;
using System.Threadin g;
namespace ComThreadTest
{
class MyConsoleApp
{
[MTAThread]
static void Main(string[] args)
{
TestClass test1 = new TestClass("1");
TestClass test2 = new TestClass("2");
Thread th = new Thread(new ThreadStart(tes t1.Go));
th.ApartmentSta te = ApartmentState. MTA;
th.Start();
th = new Thread(new ThreadStart(tes t2.Go));
th.ApartmentSta te = ApartmentState. MTA;
th.Start();
}
}
public class TestClass
{
public DotNetSleeperCl ass dotnet;
public MyCom.SleeperCl ass com;
public string mName;
public TestClass(strin g name)
{
mName = name;
dotnet = new DotNetSleeperCl ass();
com = new MyCom.SleeperCl ass();
}
[MTAThread]
public void Go()
{
DateTime start = DateTime.Now;
Console.WriteLi ne(mName + " OUT at " +
start.ToString( "hh:mm:ss:fff") );
//THIS WORK FINE BECAUSE IT'S SIMPLE MANAGED CODE.
//dotnet.Go();
//THIS BLOCKS OTHER THREADS WHILE IT RUNS com.Go();
DateTime finish = DateTime.Now;
TimeSpan ts = finish - start;
Console.WriteLi ne(mName + " IN at " +
finish.ToString ("hh:mm:ss:fff" ) + " ELAPSED=" +
ts.TotalMillise conds.ToString( ) + " milliseconds.") ;
Console.WriteLi ne("Press any key to continue...");
Console.Read();
}
}
public class DotNetSleeperCl ass
{
public DotNetSleeperCl ass() {}
public void Go()
{
Thread.Sleep(30 00);
}
}
}
*************** **** VB6 ACTIVEX DLL *************** *****
Option Explicit
Public Sub Go()
'On my box this runs in about 4 seconds to run.
Dim a, b, c
For a = 1 To 1000
For b = 1 To 1000
c = a Mod b
DoEvents
Next b
Next a
End Sub
I've got a legacy COM dll that I'd like to use in my multithreaded C#
app. Problem though is when I create several instances of the interop
object and start them on their own threads they always seem to run
synchronously.
I've boiled down the code to a simple "lab" experiment below. In this
console app you'll see that instance one comes back after roughly 4
seconds and then instance two comes back in 8 seconds, twice the time.
This indicates that it is wait on the first thread to finish. Why
won't they run concurrently?
I've tried my best to make sure that the thread types are MTAThread
before calling the COM objects. Also, in VB I made sure the project
threading model was "Apartment Threaded" and the Instancing set to
GlobalUseMulti.
Any ideas???
Thanks
*************** **** .NET C# CONSOLE TEST APP *************** *****
using System;
using System.Threadin g;
namespace ComThreadTest
{
class MyConsoleApp
{
[MTAThread]
static void Main(string[] args)
{
TestClass test1 = new TestClass("1");
TestClass test2 = new TestClass("2");
Thread th = new Thread(new ThreadStart(tes t1.Go));
th.ApartmentSta te = ApartmentState. MTA;
th.Start();
th = new Thread(new ThreadStart(tes t2.Go));
th.ApartmentSta te = ApartmentState. MTA;
th.Start();
}
}
public class TestClass
{
public DotNetSleeperCl ass dotnet;
public MyCom.SleeperCl ass com;
public string mName;
public TestClass(strin g name)
{
mName = name;
dotnet = new DotNetSleeperCl ass();
com = new MyCom.SleeperCl ass();
}
[MTAThread]
public void Go()
{
DateTime start = DateTime.Now;
Console.WriteLi ne(mName + " OUT at " +
start.ToString( "hh:mm:ss:fff") );
//THIS WORK FINE BECAUSE IT'S SIMPLE MANAGED CODE.
//dotnet.Go();
//THIS BLOCKS OTHER THREADS WHILE IT RUNS com.Go();
DateTime finish = DateTime.Now;
TimeSpan ts = finish - start;
Console.WriteLi ne(mName + " IN at " +
finish.ToString ("hh:mm:ss:fff" ) + " ELAPSED=" +
ts.TotalMillise conds.ToString( ) + " milliseconds.") ;
Console.WriteLi ne("Press any key to continue...");
Console.Read();
}
}
public class DotNetSleeperCl ass
{
public DotNetSleeperCl ass() {}
public void Go()
{
Thread.Sleep(30 00);
}
}
}
*************** **** VB6 ACTIVEX DLL *************** *****
Option Explicit
Public Sub Go()
'On my box this runs in about 4 seconds to run.
Dim a, b, c
For a = 1 To 1000
For b = 1 To 1000
c = a Mod b
DoEvents
Next b
Next a
End Sub
Comment