Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
DotNet
acad.assignment.mngr
academic
adonet
aspnet
aspnet.announcements
aspnet.build.controls
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
clr
compactframework
component_services
datatools
distributed_apps
drawing
faqs
framework
framework.wmi
general
internationalization
interop
languages.csharp
languages.jscript
languages.vb
languages.vb.controls
languages.vb.data
languages.vb.upgrade
languages.vc
languages.vc.libraries
myservices
odbcnet
performance
remoting
scripting
sdk
security
setup
vjsharp
vsa
webservi.enhancements
webservices
windowsforms
windowsforms.controls
winforms.databinding
winforms.designtime
xml
  
 
date: Thu, 14 Aug 2008 11:00:00 -0700,    group: microsoft.public.dotnet.framework        back       


STAThread is non-deterministic when debugging   
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.
date: Thu, 14 Aug 2008 11:00:00 -0700   author:   Morten Herman Langkjaer

Re: STAThread is non-deterministic when debugging   
On Thu, 14 Aug 2008 11:00:00 -0700, Morten Herman Langkjaer  
 wrote:

> [...]
> 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
date: Thu, 14 Aug 2008 11:07:08 -0700   author:   Peter Duniho

Re: STAThread is non-deterministic when debugging   
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" wrote:

> On Thu, 14 Aug 2008 11:00:00 -0700, Morten Herman Langkjaer  
>  wrote:
> 
> > [...]
> > 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
>
date: Thu, 14 Aug 2008 11:23:01 -0700   author:   Morten Herman Langkjaer

Re: STAThread is non-deterministic when debugging   
On Aug 14, 10:07 pm, "Peter Duniho" 
wrote:
> 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).
date: Thu, 14 Aug 2008 21:47:49 -0700 (PDT)   author:   Pavel Minaev

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us