OLE call STAThreadAttribute exeception on saveFileDialog.ShowDialog(this)

  • Thread starter Thread starter Stefan
  • Start date Start date
S

Stefan

Hi,

although main() has [STAThread] attribute, somehow my C#/VS2005 win
form application changed to MTA mode. Within the application I create
a backgroud process that does the work to be able to update a progress
bar in the main window. After the backgroud process is completed
(backgroundWorker_RunWorkerCompleted was called), I want to write some
info to LOG-files. Upon calling saveFileDialog.ShowDialog(this) to get
a filename, I get the STAThreadAttribute exception. Just before this
call I checked System.Threading.Thread.CurrentThread.ApartmentState
which is then MTA.

So my questions are:
- what makes my application switch to MTA
- what can I do to bring it back to STA
- why this OLE call

Thanks in advance for any hints!
Stefan

P.S.: This is a repost from
microsoft.public.dotnet.framework.windowsforms
 
Hi,

although main() has [STAThread] attribute, somehow my C#/VS2005 win
form application changed to MTA mode. Within the application I create
a backgroud process that does the work to be able to update a progress
bar in the main window. After the backgroud process is completed
(backgroundWorker_RunWorkerCompleted was called), I want to write some
info to LOG-files. Upon calling saveFileDialog.ShowDialog(this) to get
a filename, I get the STAThreadAttribute exception. Just before this
call I checked System.Threading.Thread.CurrentThread.ApartmentState
which is then MTA.

So my questions are:
- what makes my application switch to MTA
- what can I do to bring it back to STA
- why this OLE call

Thanks in advance for any hints!
Stefan

P.S.: This is a repost from
microsoft.public.dotnet.framework.windowsforms

You could do the easier thing and change the attribute to STA..
OR..

Spawn a new thread to do the file writing. BE carefull to place the
STA attrbute on the method you pass as the thread start.
this will solve your problem.


As for your question..
My theory is that .net uses legacy components for file dialog and save
dialog.
 
Thanks for your feedback.

I already tried to change back to STA. This does not work.

As my app is still under development it never occured to me to test
the thing as an EXE. There the problem does not exist. It's just when
I run/debug the app within VS IDE. If I disable the debug option "VS
host process" in the app's settings, then it also runs without
exception in the IDE (but then I cannot debug it).

I also found postings about [STAThread] not working (guess this was
correct for older versions of .NET) and the advice to set STA-mode as
the first statement in the main method. All this won't work. The app
is by then already in MTA mode and you just can't change to STA mode
by simply calling SetAppartmentState.

Stefan

although main() has [STAThread] attribute, somehow my C#/VS2005 win
form application changed to MTA mode. Within the application I create
a backgroud process that does the work to be able to update a progress
bar in the main window. After the backgroud process is completed
(backgroundWorker_RunWorkerCompleted was called), I want to write some
info to LOG-files. Upon calling saveFileDialog.ShowDialog(this) to get
a filename, I get the STAThreadAttribute exception. Just before this
call I checked System.Threading.Thread.CurrentThread.ApartmentState
which is then  MTA.
So my questions are:
- what makes my application switch to MTA
- what can I do to bring it back to STA
- why this OLE call
Thanks in advance for any hints!
Stefan
P.S.: This is a repost from
microsoft.public.dotnet.framework.windowsforms

You could do the easier thing and change the attribute to STA..
OR..

Spawn a new thread to do the file writing. BE carefull to place the
STA attrbute on the method you pass as the thread start.
this will solve your problem.

 As for your question..
My theory is that .net uses legacy components for file dialog and save
dialog.- Zitierten Text ausblenden -

- Zitierten Text anzeigen -
 
Back
Top