在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式。请确保您的 Main 函数带有 STAThreadAttr

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        private static void Main(String[] args)
        {
            System.Windows.Forms.Application.EnableVisualStyles();
            System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);

            // Application.Run(new frmMain());

            LoadInit loadInit = new LoadInit();
            loadInit.Load(args);
        }           

        private void showMainForm()
        {
            System.Threading.Thread t = new
            System.Threading.Thread(new System.Threading.ThreadStart(NewMessageLook));
            // 解决单线程无法调用选择文件对话框问题

            t.SetApartmentState(System.Threading.ApartmentState.STA);  
            t.Start();

            this.Close();
        }

说明:

红色字体部分是解决问题的关键

猜你喜欢

转载自hslh22.iteye.com/blog/1687340