I had one problem like that. The apartmentState of the main thread are [MTAThread].
At the start of the class i put these code:
public class FormWithMTA{
delegate void ModifyTextBox(string value);
private FolderBrowserDialog opn;
Thread runningThread;
...
In the event i put this:
...
opn = new FolderBrowserDialog();
runningThread = new Thread(new ThreadStart(OpenDlg));
//Change the apartmentState of that thread to work in STA if your main ApartmentState are MTA
runningThread.SetApartmentState(ApartmentState.STA);
runningThread.Start();
...
and use that part of the code for get de path in the runningThread:
private void OpenDlg()
{
opn.Description = "Escolha de diretório:";
opn.ShowNewFolderButton = false;
opn.RootFolder = System.Environment.SpecialFolder.MyComputer;
try
{
DialogResult d = opn.ShowDialog();
if (d == DialogResult.OK)
{
if (opn.SelectedPath != "")
UpdateStatus(opn.SelectedPath);
}
}
catch (InvalidCastException erro)
{
//When work in main with MTA everytime i get that exception with dialog result
if (opn.SelectedPath != "")
UpdateStatus(opn.SelectedPath);
}
catch (Exception er)
{
}
opn.Dispose();
opn = null;
runningThread.Join();
}
void UpdateStatus(string value)
{
if (txtBox.InvokeRequired)
{
//Call the delegate for this component.
txtBox.Invoke(new ModifyTextBox(UpdateStatus), new object[] { value });
return;
}
txtBox.Text = value;
}
Well, that code work for me in windows 7 64bits. In debugger and when i execute the program in the client machine.