C# COM issue : "the application has called an interface that was marshalled for a different thread”
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:
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);
for further reading: