ITPub博客

首页 > > > winform多线程牛人进.....

winform多线程牛人进.....

作者:choubou 时间:2019-11-07 13:50:24 0 删除 编辑

为什么要加上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/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-10-08

  • 博文量
    175
  • 访问量
    10707