Reply

Unable to register message filter because the current thread apartment state is not STA

[ Edited ]

I have a small DLL program to deal with some Solid Edge tasks and it runs great! in other applications am using it. But I am trying to use it in a consele application and I get this message when the dll tries to start solid edge instance.

 

This is the error I get:

Error Creating Solid Edge Application System.Runtime.Interopservices.COMException (0x80004005) Unable to register message filter because the current thread apartment state is not STA

 

I have all this code in C#.

 

Any Help from heavy programmers would be really appreciated.

 

Sergio.

7 REPLIES

Re: Unable to register message filter because the current thread apartment state is not STA

[ Edited ]

Never mind I just solved my own problem. I had to decorate the main void in my console application with [STAThread] and my DLL runs.

 

Thanks anyways.

Re: Unable to register message filter because the current thread apartment state is not STA

[ Edited ]

Sergio,

 

Glad you figured it out. Just a little background information for anyone reading this in the future, Console applications do not have the STAThread attribute applied to the Main() method by default. You must manually apply it if you want it set. In contrast, Windows Form applications do have the STAThread attribute applied by default (*The main GUI thread*). It can be confusing when looking at code examples as it's not always obvious that it is the problem.

 

Jason Newell
Applications Architect
www.jasonnewell.net

Re: Unable to register message filter because the current thread apartment state is not STA

Thanks! Jason.

Re: Unable to register message filter because the current thread apartment state is not STA

I get this error but not all the time every couple hundred occurences in a VB app.  How do I use the STAthread?  Not sure how to apply it.


Thanks,
Ken

Production: ST9 MP7
Testing: ST10

Re: Unable to register message filter because the current thread apartment state is not STA

The STAThread attribue must be applied to the Main() method of console applications. You don't have to worry about this in WinForm applications.

 

Visual Basic Example

<STAThread> _
Shared Sub Main(ByVal args() As String)

End Sub

C# Example

[STAThread]
static void Main(string[] args)
{

}

 

Jason Newell
Applications Architect
www.jasonnewell.net

Re: Unable to register message filter because the current thread apartment state is not STA

So in the top of my app I added "<STAThread()>" just above my Main sub as shown below???

 

I'm still getting the error on occasion, but am wondering if it might be due to trying to print out a draft to the Adobe PDF Printer before it is done creating the PDF from the previous Draft I sent to it (batch creating PDF's)?

 

<STAThread()>

Sub Main()


Thanks,
Ken

Production: ST9 MP7
Testing: ST10

Re: Unable to register message filter because the current thread apartment state is not STA

What error are you getting? The OP stated they were getting "Unable to register message filter because the current thread apartment state is not STA". The only way you get that is if the STAThread attribute is not present.

Jason Newell
Applications Architect
www.jasonnewell.net