そんな悩みが一つありました。メイン スレッドの apartmentState は [MTAThread] です。
クラスの開始時に、次のコードを配置します。
public class FormWithMTA{
delegate void ModifyTextBox(string value);
private FolderBrowserDialog opn;
Thread runningThread;
...
イベントで私はこれを置く:
...
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();
...
コードのその部分を使用して、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;
}
まあ、そのコードはWindows 7 64ビットで機能します。デバッガーで、クライアントマシンでプログラムを実行するとき。