473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Multithreading concurrency with Interop

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.Threading;

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(test1.Go));
th.ApartmentState = ApartmentState.MTA;
th.Start();

th = new Thread(new ThreadStart(test2.Go));
th.ApartmentState = ApartmentState.MTA;
th.Start();
}
}

public class TestClass
{
public DotNetSleeperClass dotnet;
public MyCom.SleeperClass com;
public string mName;

public TestClass(string name)
{
mName = name;
dotnet = new DotNetSleeperClass();
com = new MyCom.SleeperClass();
}

[MTAThread]
public void Go()
{
DateTime start = DateTime.Now;
Console.WriteLine(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.WriteLine(mName + " IN at " +
finish.ToString("hh:mm:ss:fff") + " ELAPSED=" +
ts.TotalMilliseconds.ToString() + " milliseconds.");
Console.WriteLine("Press any key to continue...");
Console.Read();
}
}

public class DotNetSleeperClass
{
public DotNetSleeperClass() {}

public void Go()
{
Thread.Sleep(3000);
}
}

}
******************* 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
Nov 22 '05 #1
0 769

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Mark Sisson | last post by:
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...
1
by: MSNEWS | last post by:
Hi I have developed a vb.net application which converts word documents in a directory to text files and then loads the text into a SQL SERVER database. (I have around 8 million documents to...
3
by: Alina | last post by:
Did anybody knows a way to secure the read/write multithreading and multiprocess in an xml file ? Thank you
0
by: Jana | last post by:
Hi, I have C# dll called in a VB application. And this C# dll is multithreaded, has two threads sleeping, suspended or running. When the VB application exits I "Interrupt" and "Abort" these...
0
by: LakshmiP | last post by:
Hi, I have C# dll called in a VB application. And this C# dll is multithreaded, has two threads sleeping, suspended or running. When the VB application exits I "Interrupt" and "Abort" these...
55
by: Sam | last post by:
Hi, I have a serious issue using multithreading. A sample application showing my issue can be downloaded here: http://graphicsxp.free.fr/WindowsApplication11.zip The problem is that I need to...
3
by: daan | last post by:
Hello, I have a problem and I can't get the solution for it :( I have a com dll, which i imported as a reference. The com object is part of a class which is multithreaded and will create...
7
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or...
19
by: jupiter | last post by:
Hi guys!!! Just one quick question... Which database module should I use when I want to use multi threading as my application requires lots of data from internet ???? I also want this database...
42
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I'm currently writing a program and I've got in mind to keep it as portable as possible. In particular I want it to run on Linux and Windows, but I'm also keeping an open mind to any machine that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.