Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
3.77/5 (3 votes)
See more:
Hello,

In my application, when i am trying to open a OpenFileDialog, i am getting an error as:
"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".

I did added [STAThread] attribute to my main function but still i am getting this error.

In my main form, i am opening another form as ShowDialog() and inside that form i am trying to call OpenFileDialog(), is this not allowed?

Please tell me what i am doing wrong.

Thanks,
Nagendra.
Posted
Updated 26-Nov-10 1:12am
v2
Comments
tranthuongthien 3-May-13 22:29pm    
Thread thdSyncRead = new Thread(new ThreadStart(syncRead));
thdSyncRead.SetApartmentState(ApartmentState.STA);
thdSyncRead.Start();
tranthuongthien 3-May-13 22:30pm    
Bạn cần thêm dòng lệnh (command) thdSyncRead.SetApartmentState(ApartmentState.STA);
ở bên dưới Thread như thế là được . Tôi đã thử và thành công.
Vishwaeet 13-Jan-15 1:21am    
Great solution. It worked for me in asp.new web application.
Member 11638754 2-May-15 6:42am    
Ya bro... u r right...
Ajayk3 18-Apr-20 8:46am    
Working file, it is need to be used in the thread which is running and giving error

Hi,

see this thread on MSDN that shows several possible solutions. See if one of them helps you.

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/2411f889-8e30-4a6d-9e28-8a46e66c0fdb/[^]
 
Share this answer
 
Comments
nagendrathecoder 26-Nov-10 7:11am    
Thanks man, i used the solution of Threading from that link and it solved my problem.
public class Invoker
{
public CommonDialog InvokeDialog;
private Thread InvokeThread;
private DialogResult InvokeResult;

public Invoker(CommonDialog dialog)
{
InvokeDialog = dialog;
InvokeThread = new Thread(new ThreadStart(InvokeMethod));
InvokeThread.SetApartmentState(ApartmentState.STA);
InvokeResult = DialogResult.None;
}

public DialogResult Invoke()
{
InvokeThread.Start();
InvokeThread.Join();
return InvokeResult;
}

private void InvokeMethod()
{
InvokeResult = InvokeDialog.ShowDialog();

}
}


Usage

SaveFileDialog dlg = new SaveFileDialog();
if (DialogResult.OK == (new Invoker(dlg).Invoke())) { /*handle result*/ ;}
 
Share this answer
 
v2
Comments
[no name] 26-Jun-14 10:23am    
The question asked and answered 4 years ago. Why are you digging up dead questions to answer?
Member 10137996 12-Dec-14 18:50pm    
//SaveFileDialog dlg = new SaveFileDialog();
FolderBrowserDialog folderBrowserDlg = new FolderBrowserDialog();
if (DialogResult.OK == (new Invoker(folderBrowserDlg).Invoke()))
{

// A new folder button will display in FolderBrowserDialog.
folderBrowserDlg.ShowNewFolderButton = true;
//Show FolderBrowserDialog


>>>//get error DialogResult dlgResult = folderBrowserDlg.ShowDialog();
if (dlgResult.Equals(DialogResult.OK))
{
//Show selected folder path in textbox1.
textBox1.Text = folderBrowserDlg.SelectedPath;
//Browsing start from root folder.
Environment.SpecialFolder rootFolder = folderBrowserDlg.RootFolder;
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900