原網址 http://blog.csdn.net/fisher_china/article/details/6043475
//先製作一個DialogState的類別
public class DialogState
{
public DialogResult result;
public FileDialog dialog;
public void ThreadProcShowDialog()
{
result = dialog.ShowDialog();
}
}
//在你呼叫SaveFileDialog的類別裡面,加入這個方法
private DialogResult STAShowDialog(FileDialog dialog)
{
DialogState state = new DialogState();
state.dialog = dialog;
System.Threading.Thread t = new System.Threading.Thread(state.ThreadProcShowDialog);
t.SetApartmentState(System.Threading.ApartmentState.STA);
t.Start();
t.Join();
return state.result;
}
调用时:(使用時)
OpenFileDialog openFileDlg= new OpenFileDialog();
openFileDlg.InitializeLifetimeService();
openFileDlg.Filter = "Config Files (*.config)|*.config| (*.xml)|*.xml";
openFileDlg.Title = "选择配置文件";
DialogResult ret = STAShowDialog(openFileDlg); //使用多執行緒的方式執行ShowDialog
if (ret == DialogResult.OK)