473,796 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Single threaded class for COM interop

Rob
Hi.

I'm having difficulty calling a 3rd party COM object that must be
called in the apartment (single) threaded model. As far as I know,
there are 3 ways todo this:

1 - use "aspcompat=true " for aspx pages
2 - use [STAThread] attribute for windows/console applications
3 - use System.Thread.C urrentThread.Ap artmentState =
ApartmentState. STA

The problem is that the COM object is only called through a business
layer module. This business layer consists of a number of classes that
encapsulate the calls to the COM object. Therefore, option 1 and 2 are
not applicable and option 3 does not work since the threading model
has already been defined even before the class initializer is called.

One solution that I've come up with is calling a 'stub' method for
each method in the business layer. This 'stub' will declare a new
thread, set the apartment model to STA, then start the thread. This is
tedious because every method will require a 'stub' and all parameters
(in and out) must be handled through private class members. I can't
imagine that this is the proper way of doing things.

So, my question is: Is there a way to make the entire class
single-threaded by default (similar to [STAThread] for a windows app)?

Any help would be greatly appreciated.

Rob.
Nov 18 '05 #1
2 1710
no, if a class is not thread safe, it has no way of enforcing it, it up to
the caller. the 3 methods you list are for ensuring the caller (a thread)
does not do a thread switch.

your solution is the correct one, except, that the stubs should only create
one STA thread, then switch to that thread to make the call, unless the com
object is completly stateless.

-- bruce (sqlwork.com)
"Rob" <ro**********@h otmail.com> wrote in message
news:ad******** *************** **@posting.goog le.com...
Hi.

I'm having difficulty calling a 3rd party COM object that must be
called in the apartment (single) threaded model. As far as I know,
there are 3 ways todo this:

1 - use "aspcompat=true " for aspx pages
2 - use [STAThread] attribute for windows/console applications
3 - use System.Thread.C urrentThread.Ap artmentState =
ApartmentState. STA

The problem is that the COM object is only called through a business
layer module. This business layer consists of a number of classes that
encapsulate the calls to the COM object. Therefore, option 1 and 2 are
not applicable and option 3 does not work since the threading model
has already been defined even before the class initializer is called.

One solution that I've come up with is calling a 'stub' method for
each method in the business layer. This 'stub' will declare a new
thread, set the apartment model to STA, then start the thread. This is
tedious because every method will require a 'stub' and all parameters
(in and out) must be handled through private class members. I can't
imagine that this is the proper way of doing things.

So, my question is: Is there a way to make the entire class
single-threaded by default (similar to [STAThread] for a windows app)?

Any help would be greatly appreciated.

Rob.

Nov 18 '05 #2
Rob
Thanks Bruce.

"bruce barker" <no***********@ safeco.com> wrote in message news:<#z******* *******@TK2MSFT NGP12.phx.gbl>. ..
no, if a class is not thread safe, it has no way of enforcing it, it up to
the caller. the 3 methods you list are for ensuring the caller (a thread)
does not do a thread switch.

your solution is the correct one, except, that the stubs should only create
one STA thread, then switch to that thread to make the call, unless the com
object is completly stateless.

-- bruce (sqlwork.com)
"Rob" <ro**********@h otmail.com> wrote in message
news:ad******** *************** **@posting.goog le.com...
Hi.

I'm having difficulty calling a 3rd party COM object that must be
called in the apartment (single) threaded model. As far as I know,
there are 3 ways todo this:

1 - use "aspcompat=true " for aspx pages
2 - use [STAThread] attribute for windows/console applications
3 - use System.Thread.C urrentThread.Ap artmentState =
ApartmentState. STA

The problem is that the COM object is only called through a business
layer module. This business layer consists of a number of classes that
encapsulate the calls to the COM object. Therefore, option 1 and 2 are
not applicable and option 3 does not work since the threading model
has already been defined even before the class initializer is called.

One solution that I've come up with is calling a 'stub' method for
each method in the business layer. This 'stub' will declare a new
thread, set the apartment model to STA, then start the thread. This is
tedious because every method will require a 'stub' and all parameters
(in and out) must be handled through private class members. I can't
imagine that this is the proper way of doing things.

So, my question is: Is there a way to make the entire class
single-threaded by default (similar to [STAThread] for a windows app)?

Any help would be greatly appreciated.

Rob.

Nov 18 '05 #3

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

Similar topics

8
2418
by: Matthew Bell | last post by:
Hi, I've got a question about whether there are any issues with directly calling attributes and/or methods of a threaded class instance. I wonder if someone could give me some advice on this. Generally, the documentation suggests that queues or similar constructs should be used for thread inter-process comms. I've had a lot of success in doing that (generally by passing in the queue during the __init__ of the thread) and I can see...
3
2006
by: Robb Gilmore | last post by:
We inherited an ASP.NET application which is not thread-safe. Many copies of this application are in the field and customers are having problems, which we believe is due to the thread safety problem. Is there any way to configure IIS to run single-threaded, so that these customers can get along without errors until we can get them updated software? Some will have IIS 5.0 ( Win2K and WinXP ) and some will have IIS 6.0 ( Win03 ). For...
3
1605
by: Martin Ink | last post by:
I have a program linked to an .exe and this program is started from an other program. I would like to control the execution of the program so that the program only runs 'single threaded'. I don't want simultaneously session of my program to be run. My program must be an .exe. Is there a way to control this in a simple way ?? Rgds
5
2530
by: Ranju. V | last post by:
Hi, I use a lot asynchronous XMLHTTP from JavaScript. Are the callbacks that we register with XMLHTTP guaranteed to execute single-threaded? For instance, consider the following code snippet. var xmlhttp1 = new ActiveXObject( "Msxml2.XMLHTTP.4.0" ); xmlhttp1.onreadystatechange = callback; xmlhttp1.open( "POST", "foo.aspx", true ); xmlhttp1.send( data1 );
6
4964
by: ben | last post by:
I am needing a web service to be single threaded. Is this possible? Any ideas would be helpful
3
2607
by: Rsrany | last post by:
I've been working on a few gtk applications and need to tie a hot key catcher into a thread. I am currently finding threaded user32.GetMessageA do not work. I have included two programs: 1) a non-threaded version that works 2) a threaded version that doesnt work. Any constructive suggestions would be helpful
0
929
by: mhdostal | last post by:
I have a multi-threaded application using both managed (C#/.NET) and unmanaged code (C++). The unmanaged code calls into the manage code (via COM Interop) to perform a variety of tasks. Every two or three days the application would unexpectedly crash; I added a GC.Collect() call to the end of every method in the module in question and now the problem happens 4 -5 time a day. Some debug information from WinDbg is below. I'm currently...
0
1603
by: mike1reynolds | last post by:
While my project is in an STA state, when I added a class with no form it is executing in an MTA state. I can't find any reference to MTA or STA anywhere in the entire solution. How do I set a class to run in STA rather than MTA mode? When I try to instantiate a form object from a class with no form, I get an exception of "control cannot be instantiated because the current thread is not in a single-threaded apartment". When I put...
11
1735
by: Chris Thomasson | last post by:
Here is the code: http://pastebin.com/m4a405e67 One advantage to using a region allocator is that you can usually skip most calls to free. Instead you can merge multiple free calls into a single reset call. Here is simple example: ___________________________________________________________________
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10465
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10242
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.