STAThread is non-deterministic when debugging

  • Thread starter Morten Herman Langkjaer
  • Start date
M

Morten Herman Langkjaer

I have an application written in C#, that is a windows application. in the
application there is a treeview that allows drag-n-drop functionality. To
enable drag-n-drop, the main thread of the application hasto be a single
threaded apartment, so i marked my main method with the [STAThread] attribute.

When starting my application, i received an exception that the main thread
should be Single threaded apartment, and to my surprise, i found that the
Thread.CurrentThread.GetApartmentState() operation returned MTA.

I tried to create a new project for testing this issue, only having a single
program file, instead of a large project. To my surprise, the new test
project had STA for the current thread, when i started debugging the main
method.

After that i tried to copy all my code files into my new test project, and
then debug again, and then my application suddenly had STA on the main
thread. Since my demo project was located in the "my documents\visual
studio..." folder, i decided to move the folder containing the demo project,
including the product code, to the folder structure of my configuration
management system. After moving the project, i tried to debug my demo project
again, and once again, i was surprised to see that the maain thread now was
MTA again. It was the same source and the same project file, that in a
different location had a different result.

Later i foun out, that if i run the application without the debugger
attached, the apartmentstate is STA, but if i debug, the apartmentstate is
MTA.

Is there anyone that can help me with this issue? could it have something to
do with the fact the debugging causes the Visual studio hosting proces to
execute my application and that it could possibly be startet in a multi
threaded apartment?

I hope somebody can help me, i would really like to debug my application.
 
M

Morten Herman Langkjaer

Hi, thanks for the quick reply. As i wrote in the above, i managed to get the
large project running in STA mode, when the project and source files was
located in the "My Documents" folder, but when i moved the project and source
files to a different location, the application was suddenly runing in MTA
mode.

/Morten

Peter Duniho said:
[...]
Later i foun out, that if i run the application without the debugger
attached, the apartmentstate is STA, but if i debug, the apartmentstate
is
MTA.

Is there anyone that can help me with this issue? could it have
something to
do with the fact the debugging causes the Visual studio hosting proces to
execute my application and that it could possibly be startet in a multi
threaded apartment?

I hope somebody can help me, i would really like to debug my application.

It sounds as though you have something else that is somehow managing to
execute on that main thread and is initializing COM before the managed
Main() method gets to execute. How that would happen, I have no idea. It
seems to me that it shouldn't be possible, but maybe there's something
interesting and unusual about the larger project you're dealing with.

I'm sorry that I don't have anything more concrete than that, but maybe
that's enough to get you looking in the right direction? Good luck!

Pete
 
P

Pavel Minaev

It sounds as though you have something else that is somehow managing to  
execute on that main thread and is initializing COM before the managed  
Main() method gets to execute.  How that would happen, I have no idea

It could be a static initializer of some class which is referenced,
directly or indirectly, by the class containing Main (or it could be
some class marked as beforefieldinit, in which case the initialization
can happen at any time before the first use, including before Main).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top