首页 > > > winform多线程牛人进.....
为什么要加上tr.SetApartmentState(ApartmentState.STA);这句。否则会报下面那个错!
请详细回答!
using System;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
}
private Thread tr;
void Form1_Load(object sender, EventArgs e)
{
tr = new Thread(new ThreadStart(Do));
tr.SetApartmentState(ApartmentState.STA);
tr.IsBackground = true;
tr.Start();
}
private void Do()
{
System.Windows.Forms.SaveFileDialog s = new SaveFileDialog();
if (s.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
MessageBox.Show("123");
}
//在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式。
//请确保您的 Main 函数带有 STAThreadAttribute 标记。 只有将调试器附加到该进程才会引发此异常。
}
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4548/viewspace-3139858/ ,如需转载,请注明出处,否则将追究法律责任。