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”.

So:

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
{
[STAThread]
static void Main(string[] args)
{
}
}
}

Furthermore:
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)

{
timer.Stop();

//Classes.IDX_PI_Processing oProcessing = new Classes.IDX_PI_Processing(config);   //These have now been moved to a new thread due to the issue
//oProcessing.ProcessPIQueue();
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
t.Start();
t.Join();

timer.Start();
}

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

<a href="https://www.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

Electromagnetic Compatibility (EMC), Bonding and Earthing

Industrial environments contain electrical equipment that can generate interference and affect the performance of industrial communication networks. Correct cable installation, shielding, bonding and earthing can help reduce interference and improve network...

Siemens PROFIBUS Diagnostic Repeater Termination Guide

The Siemens Diagnostic Repeater provides additional diagnostic capabilities that can help detect and locate line faults while a PROFIBUS network is operating. Correct configuration, wiring and termination are important for both network communication and effective...

Siemens PROFIBUS Standard Repeater Termination Guide

Repeaters are used in PROFIBUS networks to link segments, extend network distances and regenerate signals. Correct installation, wiring and termination are important for maintaining reliable communication between network segments. This technical specification provides...

Control Network Permanent Monitoring

Industrial control network faults can develop over time and may not always be immediately visible to maintenance teams. Permanent monitoring provides continuous visibility into network health, helping identify developing issues so that they can be addressed before...

Technical Specifications and Standards Index

Industrial control networks are designed, installed and maintained according to a range of technical specifications and recognised industry standards. Having a central reference makes it easier to identify the documents and standards relevant to a particular...

Sign up for our newsletter

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

Related Posts

We understand the importance of minimising downtime and maximising productivity in your plant. Through our innovative solutions, we work alongside you to streamline processes, enhance efficiency, and reduce costly downtime.

CONTACT US

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

JOIN OUR COMMUNITY

Copyright @2025 Industrial Data Xchange. All rights reserved.