I recently updated a simple program that uses a filedialog control to locate
a file. When I try and run the following code I get this error:
Current thread must be set to single thread apartment (STA) mode before OLE
calls can be made. Ensure that your Main function has STAThreadAttribute
marked on it. This exception is only raised if a debugger is attached to the
process.
Here is the code
this.openFileDialog1.FileName = this.txtCompilePath.Text;
DialogResult result = openFileDialog1.ShowDialog(this);
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.txtCompilePath.Text = openFileDialog1.FileName;
}
|
| DollarBill Monday, April 09, 2007 1:33 PM |
Add the STAThreadAttribute attribute on the Main method. This attribute is required if your program access OLE related functions, like Clipboard class does.
C#
[ STAThread]
static void Main(string[] args)
{
}
Visual Basic
< STAThread()> _
SharedSub Main(args As String())
EndSub
|
| Cristian_t Monday, April 09, 2007 2:11 PM |
Add the STAThreadAttribute attribute on the Main method. This attribute is required if your program access OLE related functions, like Clipboard class does.
C#
[ STAThread]
static void Main(string[] args)
{
}
Visual Basic
< STAThread()> _
SharedSub Main(args As String())
EndSub
|
| Cristian_t Monday, April 09, 2007 2:11 PM |
I was having same problem so, following solution posted at link below, I simply deleted all dlls (there were a few old ones) in my output folder then rebuilt it and it worked.
http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework.windowsforms/topic36723.aspx |
| philipsh Monday, October 15, 2007 7:43 PM |
My main function already had [STAThread] above it, but the URL provided in the previous message was what I needed. There was just an old DLL in my bin\debug folder that had to be deleted. |
| Greg Wishart Thursday, January 10, 2008 4:46 AM |
I get the same error aswel.
[STATThread] is already on top of the main method
Which "old DLL" are you referring to?
Thanks
EThan
|
| ethanbh Sunday, June 08, 2008 5:57 PM |
Hi,
I am using a clipboard function in my webpage.
It gives me the same error
I dont have as such any main fnction in my program.
I tried placing [STATThread] before the event handler of the button click event.
On this button click text on the webpage has to be coped to clipboard
I need help regarding this |
| t-vks Tuesday, June 24, 2008 12:21 PM |
After I deleted my used DLL files, everything is going fine. |
| Cowbay Saturday, July 19, 2008 8:16 PM |
Hi, my project is a C# .NET website, so it doesn't have a main(). How do I apply the advice to set STA mode if there's no main() entry point?
Thanks,
- Steve
|
| Steve9978482 Tuesday, August 26, 2008 2:07 AM |
Ditto with Steve and t-VKS. If anyone knows the solution, please post.
Thanks, Phil
|
| philwe Friday, August 29, 2008 3:59 PM |
Ditto with Steve and t-VKS. If anyone knows the solution, please post.
Thanks, Phil
Hi I am also facing the same isue can anyone help me out |
| Rameshwariv Friday, April 03, 2009 11:35 AM |
RESPUESTA AL PROBLEMA DE LA EXCEPCIÓN Exception System.Threading.ThreadStateException Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process. SOLUCIÓN
[
STAThread] //MUY NECESARIO PARA EJECUTAR LOS métodos OLE y COM
private voidMetodoCualquiera() //no necesariamente el main, solo el que llama al hilo que llama al dialog
{
Thread
hilo =
hilo =
hilo =
new Thread(MetodoALlamar);
hilo.IsBackground =
true;
hilo.SetApartmentState(
ApartmentState.STA); //evitamos la excepción, no se bién porque
hilo.Start(IVAVentas);
}
medio confuso pero ahi está
|
| JuanicoX Thursday, September 17, 2009 11:04 PM |