ShowDialog 在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式。请确保您的 Main 函数带有 STAThreadAttribute 标记。 只有将调试器附加到该进程才会引发此异常。
代码如下 private void btnSelect_Click(object sender, EventArgs e)
{
System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(OpenFiles));
Initlv_Excel();
th.Start();
}
private void OpenFiles()
{
openExcelDialog.Filter = "Text File (*.xls)|*.xls";
this.openExcelDialog.ShowDialog();这里报错
string fileName = this.openExcelDialog.FileName;
txtFile.Text = fileName; FileListBind(fileName);
}
代码如下 private void btnSelect_Click(object sender, EventArgs e)
{
System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(OpenFiles));
Initlv_Excel();
th.Start();
}
private void OpenFiles()
{
openExcelDialog.Filter = "Text File (*.xls)|*.xls";
this.openExcelDialog.ShowDialog();这里报错
string fileName = this.openExcelDialog.FileName;
txtFile.Text = fileName; FileListBind(fileName);
}
解决方案 »
- propertygrid展开struct类型的问题
- 【求助】客户端发送数据 服务端如何提取
- c#高级编程中如何自定义异常处理,如何运用
- 求助。xls文件导入数据库时,一列中数字项、字母项只能“你死我活”??
- UltraGrid导出excel保留小数点位数的问题
- 如何用c#将image对象保存至数据库image字段
- 请问继承的类不能和原来的类进行转换么?
- SQL语句的构造问题
- C#中如何用Clipboard中的Bitmap填充图形?
- 关于C#代码操作远程2003服务器的数据库和FTP的问题,很奇怪,请高手解答
- 关于显示时间的功能问题
- dataGridView1显示Dataset的内容,用哪一句???
你的Main带[STAThread]了吗?
如果还不行,在入口程序试试加上[STAThread]
如果还不行,在程序入口试试加上[STAThread]
public void main()
{}
有的
th.SetApartmentState(System.Threading.ApartmentState.STA);
th.Start();
[quote必须将当前线程设置为单线程单元(STA)模式
你要是这么设置了,那还不如使用Show代替ShowDialog()呢,因为都不会等待阻塞线程。