C# COM issue: “The application has called an interface that was marshalled for a different thread”

7 September 2009

Hello people. I bled a little on something, so I thought I might share and save you some pain:

When calling code in a com class (for example, when using an external dll written in some other language (vb, for example), the implicit threading can be an issue).

VB by default uses STA threading (Single thread apartment), which loosely means that the application runs in one thread. If you use the above class in C#, which by default uses MTA (Multi-thread Apartment), You get an error: “The application has called an interface that was marshalled for a different thread”.


When you use such a dll in c#, you need to add the [STAThread] directive above your main program.cs file as follows:

namespace MYIDXApp
class TimeServer
static void Main(string[] args)

There is a “feature” in c# that even though you specify that the main thread runs under STAThread, any timer events happen as MTAThread.

Thus, for a timer event, you must launch a new thread under STAThread as follows:

void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)


//Classes.IDX_PI_Processing oProcessing = new Classes.IDX_PI_Processing(config);   //These have now been moved to a new thread due to the issue
Thread t = new Thread(new ThreadStart(StartNewStaThread));
t.SetApartmentState(ApartmentState.STA); // this has to be done BEFORE the thread   //starts, else it is too late


private void RunProcessingClass()
Classes.IDX_PI_Processing oProcessing = new Classes.IDX_PI_Processing(config);

Categories: Articles
<a href="https://blog.idx.co.za/author/idx-team/" target="_self">IDX Team</a>

IDX Team

Technical Team

The IDX team is a dynamic group of professionals dedicated to delivering cutting-edge industrial ICT solutions. With a focus on innovation, they leverage their expertise to streamline processes and drive efficiencies for your plant data.

Show your love!

Recent Posts

Driving Productivity with LineView Training

In light of our partnership with LineView Solutions, our team has taken up the opportunity to equip themselves with insights into the LineView system and its impact on the industry. “LineView is a production line monitoring and recording system that automatically...

Exciting New Launch: Onsite ProfiDay

IDX is excited to announce the launch of our onsite ProfiDay offering. This is available to all industries and companies that utilise PROFIBUS and/or PROFINET within their industrial communications systems. What is ProfiDAY? An exclusive onsite program offering...

Introducing our Partnership with LineView Solutions

We are excited to announce our partnership with LineView Solutions, a leading UK-based company specialising in smart factory solutions for optimising production processes. LineView developed a cutting-edge software suite designed to automatically gather data from the...

Optimise PROFIBUS and PROFINET Network Performance

As a field technician handling PROFIBUS and PROFINET systems, it is crucial to recognise the common errors that can result in costly downtime. From improper network configuration to neglecting regular maintenance, these mistakes can greatly affect system performance...

Delighted Customer Shares Experience

We recently assisted a client in rectifying damage to the Anybus ABC4090 Communicator caused by incorrect power usage onsite. We arranged for the device to be sent to HMS in Sweden for repairs, and they restored it before promptly returning it to us. Here are some...

Price Decrease on PROFIBUS Connectors

PROFIBUS connectors play a crucial role in industrial automation and data exchange systems by facilitating smooth communication between devices and networks. We are pleased to announce a significant price decrease on all our PROFIBUS connectors. Thanks to recent...

PROFIBUS Standards: Functional bonding and shielding

Electromagnetic Interference (EMI) and earthing issues can significantly impact the performance of your network. By following these six proven recommendations, you can ensure that your network is always up and running.

PROFIBUS Standards: Terminations

What is the Volts Direct Current (VDC) required for optimal operation of a PROFIBUS DP termination circuit? Get the answers you need in our latest video on PROFIBUS Standards: Terminations. Don't miss out on this essential knowledge to boost your system's performance!

PROFIBUS Standards: Segment Cable Lengths

Finding the perfect balance between PROFIBUS cable length and network speed is crucial. While longer cable segments might seem tempting, they can lead to signal degradation and slower speeds. Conversely, too short segments might limit your layout options. Striking the...

Sign up for our newsletter

Get exclusive updates and a sneak peek into our world every two months. Stay connected, stay informed.

Related Posts

Our expertise lies in establishing, maintaining, and leveraging plant data for business benefit. Through our innovative solutions, we work alongside you to streamline processes, enhance efficiency, and reduce costly downtime.


Main: +27 (11) 548 9960
Sales: +27 (11) 548 9970


Copyright @2024 Industrial Data Xchange. All rights reserved.