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();
}

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

Building Success on the Fundamentals of Operational Excellence

In the previous article, we explored how operational excellence extends beyond traditional management, emphasising a culture of continuous improvement. We highlighted the importance of industrial communication systems in enhancing real-time decision-making and...

Reduce Energy Consumption in Buildings with AC Cloud Control

Intesis’ Air Conditioning (AC) Cloud Control is a cloud-based remote-control system that allows comfortable and intuitive control of air conditioners and heat pumps. Based on a brand-agnostic cloud-based platform, it brings remotely accessible smart intelligent...

Revolutionising HVAC systems for a more sustainable future

Heating, Ventilation, and Air Conditioning (HVAC) systems are key in buildings' energy consumption and greenhouse gas emissions. With buildings responsible for over one-third of global energy consumption, optimising HVAC systems is crucial for a sustainable future....

Steady Growth in Industrial Network Market

Market shares 2024 according to HMS Networks – fieldbus, industrial Ethernet and wireless.  Every year, HMS Networks conducts a comprehensive analysis of the industrial network market, aiming to estimate the distribution of new connected nodes by type and protocol...

Moving Beyond Master and Slave Terminology

The terms "master" and "slave" have long been used in technology to describe the relationship between a controlling device (master) and a subordinate or controlled device (slave). This terminology, while deeply embedded in various technical disciplines, has its roots...

An Introduction to Operational Excellence

Operational excellence goes beyond mere operational management. It represents a strategic mindset and a culture of continuous improvement, innovation, and customer-centricity, striving for excellence in all operational and business performance aspects. Trends and...

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? A free onsite training programme, where...

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.

CONTACT US

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

SOCIAL MEDIA

Copyright @2024 Industrial Data Xchange. All rights reserved.