
Signature
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Hi Peter,
yep this piece of code works also fine for me. With my Windows application i
dont get a STA. Here is all my main code (application entry point).
static class BodeMain
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
bool ownsMutex;
// check if no other Process using a bode device with a named
mutex
using (Mutex mutex = new Mutex(true,
Omicron.BodeAnalyzer.CommonDefs.MidLayerCommonDefs.BodeMutexName, out
ownsMutex))
{
System.Reflection.Assembly assembly =
System.Reflection.Assembly.GetExecutingAssembly();
if (ownsMutex)
{
string version = "Version ";
#if DEBUG
version += assembly.GetName().Version.ToString();
#else
version += assembly.GetName().Version.ToString(2);
#endif
SplashScreen.ShowSplashScreen(version,
assembly.GetName().Version.Revision == 1);
BodeAnalyzer.BodeApplication app = new
Omicron.BodeAnalyzer.BodeApplication();
app.ScanAndSelectFirstDevice();
MainFrame theMainForm = new MainFrame(app);
SplashScreen.SetStatus("Calibrating ...");
theMainForm.BodeApplication.InitDevice(false);
// init the form
theMainForm.Init();
if (args.Length > 0)
{
theMainForm.OpenFile(args[0]);
}
// set the stop freq to fmax (different fmax for FRA and
Bode)
(theMainForm.BodeApplication.Document.TestForms[2].Test.Settings as
Omicron.BodeAnalyzer.Measurement.Sweep.SweepTest.TestSettings).SetFstopToFmax();
theMainForm.InitTimers();
SplashScreen.SetStatus("Start the Test ...");
SplashScreen.CloseForm();
theMainForm.RestorePosition();
theMainForm.BringToFront();
Application.Run(theMainForm);
// the test is startet in the OnMainFormLoad Handler
// release the mutex after closing the application
mutex.ReleaseMutex();
}
else
{
ResourceManager rm = new
ResourceManager("Omicron.BodeAnalyzer.BodeAnalyzerResources", assembly);
MessageBox.Show(rm.GetString("MsgBoxStartUpError"),
rm.GetString("MsgBoxCaptionError"), MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
} // end using
}
}
This results in a MTA and not a STA. If I create a STA thread in my main
function and execute all that code in that thread everything works fine. See
below.
static class BodeMain
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Thread mainThread = new Thread(new
ParameterizedThreadStart(RunBodeApplication));
mainThread.Name = "Bode Analyzer Suite main thread";
mainThread.SetApartmentState(ApartmentState.STA);
mainThread.Start(args);
mainThread.Join();
}
static void RunBodeApplication(object mainArguments)
{
bool ownsMutex;
string[] arguments = (string[])mainArguments;
// check if no other Process using a bode device with a named
mutex
using (Mutex mutex = new Mutex(true,
Omicron.BodeAnalyzer.CommonDefs.MidLayerCommonDefs.BodeMutexName, out
ownsMutex))
{
...
}
}
}
Can you explain me that behaviour?
Thanks,
Thomas
> Hi
>
[quoted text clipped - 29 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.
"Peter Huang" [MSFT] - 18 Nov 2005 03:26 GMT
Hi
From the code check, we will know that the winform application will also
run the STA.
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(System.Environment.Version.ToString());
MessageBox.Show(System.Threading.Thread.CurrentThread.GetApartmentState().To
String());
}
For your scenario, I guess your component may not be work well in a Winform
UI thread, that is why it will run in your spawned thread.
Best regards,
Peter Huang
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
ThomasBechter - 18 Nov 2005 07:24 GMT
Hi
A default windows application runs in STA on my machine too.
There musst be a few scenarios where the framework changes the apartment
state to MTA although It is explicity defined as STA. Can you tell me this
circumstances?
This would help me to find out why it is changed to MTA in my code.
Regards,
Thomas
> Hi
>
[quoted text clipped - 18 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.
Willy Denoyette [MVP] - 19 Nov 2005 00:32 GMT
What exactly is your (COM?) component doing before you run your theMainForm,
if this component calls CoUnitialize(), it will tear down the main thread's
apartment, the CLR will initialize the apartment to MTA at the next call
into COM. This is the only way to change the apartment state of a thread.
I'm not sure this behavior was different before v2.0, anyway you could
attach a native debugger and set a breakpoint on CoUnitialize(), normaly
this should only be called when the process terminates.
Willy.
> Hi
> A default windows application runs in STA on my machine too.
[quoted text clipped - 32 lines]
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
ThomasBechter - 21 Nov 2005 10:16 GMT
Now I found the problem. It has nothing to do with the CoUnitialize().
I unchecked the "Enable the Visual Studio hosting process" for my projekt an
everything is working fine. Thats the case for my old VS2003 projekt which I
converted to VS2005.
For evaluation I added a new project (real VS2005 project) to my solution
and paste in exactly the same main code as I had in my existing project. If I
run this new application with "VS hosting process" enabled everything working
fine too.
So there musst be a different handling of "converted" and "real" VS2005
projects.
Regards,
Thomas
> What exactly is your (COM?) component doing before you run your theMainForm,
> if this component calls CoUnitialize(), it will tear down the main thread's
[quoted text clipped - 42 lines]
> >> This posting is provided "AS IS" with no warranties, and confers no
> >> rights.
Willy Denoyette [MVP] - 20 Nov 2005 13:58 GMT
I don't get it. The hosting process is only used when running your
application from within VS, it's a convenience to reduce start-up times.
When you run your application outside VS it doesn't use the hosting process
at all.
Did you try to run your application from the explorer (double click the
*.exe)?
Willy.
> Now I found the problem. It has nothing to do with the CoUnitialize().
>
[quoted text clipped - 68 lines]
>> >> This posting is provided "AS IS" with no warranties, and confers no
>> >> rights.
ThomasBechter - 21 Nov 2005 16:57 GMT
Yes exactly, this problem occures only if I run the application within the
VS. The VS host process change the apartment state to MTA. If I start my
application from the explorer everthing works fine.
I disabled the VS host process for my project and now I can debug my
application again. -> I solved my problem :)
What I tried to explain in my last post is that this VS host process isn't
working for projects using COM interop which are created with the VS2003
(imported to VS2005). If I create a new VS2005 project with exactly the same
code as the VS2003 project this VS hosting process works.
Did you get it? Sorry for my bad english ;)
Thomas
> I don't get it. The hosting process is only used when running your
> application from within VS, it's a convenience to reduce start-up times.
[quoted text clipped - 77 lines]
> >> >> This posting is provided "AS IS" with no warranties, and confers no
> >> >> rights.
Willy Denoyette [MVP] - 21 Nov 2005 10:23 GMT
Yep, thanks for the update. Do you mind to post an issue at :
http://lab.msdn.microsoft.com/productfeedback/default.aspx
Willy.
> Yes exactly, this problem occures only if I run the application within the
> VS. The VS host process change the apartment state to MTA. If I start my
[quoted text clipped - 103 lines]
>> >> >> This posting is provided "AS IS" with no warranties, and confers no
>> >> >> rights.
"Peter Huang" [MSFT] - 19 Nov 2005 02:35 GMT
Hi
Based on my research, I did not find the known issue about the STA main
thread will be turned into MTA.
For your scenario, can you provide a simple reproduce sample and send to me
via removing "online" from my email address.
BTW: winform UI main thread is commonly running under STA thread because
winform's control is not thread-safe by default.
Best regards,
Peter Huang
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.