子线程使用FolderBrowserDialog的问题延伸2012-01-02 csdn博客 jinjazzQ:子线程如何使用FolderBrowserDialogA:
private void button1_Click(object sender, EventArgs e) ...{ System.Threading.Thread s = new System.Threading.Thread(new System.Threading.ThreadStart(test)); s.ApartmentState = System.Threading.ApartmentState.STA; s.Start(); } public void test() ...{ System.Windows.Forms.FolderBrowserDialog dlg = new FolderBrowserDialog(); dlg.ShowDialog(); }
以上代码简单的演示了FolderBrowserDialog在子线程中的使用,其中设置线程的ApartmentState为System.Threading.ApartmentState.STA是关键的语句。在.net2.0中应该使用s.SetApartmentState(System.Threading.ApartmentState.STA);如果没有上述设置会报如下错误在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式。如果你了解com的线程模型的话,应该已经清楚上面的问题根本了。