c# - System.Threading.ThreadStateException: how to solve it?
问题描述
I have a Windows forms Application to open the file dialog, and then view xml files and xlsx files in a datagridview
. I wrote the following code, Is my code OK or I need to do something more?
namespace MyFirstWindos
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
openFD.InitialDirectory = "C:";
openFD.Title = "Insert an File";
openFD.FileName = "";
openFD.Filter = "XML Files |*.xml| XLSX Files |*.xlsx ";
if (openFD.ShowDialog() == DialogResult.OK)
{
DataSet ds = new DataSet();
ds.ReadXml(openFD.FileName);
dataGridView1.DataSource = ds.Tables[0];
}
else
{
MessageBox.Show("Operation Cancelled");
}
}
}
}
namespace MyFirstWindos
{
public static class Program
{
public static void Main()
{
Compare_D_A d_A = new Compare_D_A();
d_A.CompareD_A();
// Compare_D_R d_R = new Compare_D_R();
// d_R.compareD_R();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
I have this exception
"System.Threading.ThreadStateException: 'The current thread must be set to single thread containment (STA) mode before OLE calls can be made. Make sure that the Main function is marked with STAThreadAttribute.
This exception is only activated if a troubleshooting program is linked to the process, at this row in my code if (openFD.ShowDialog() == DialogResult.OK)
.
How I can correct it and what it means?
解决方案
推荐阅读
- python - 关于返回对象类型的 Python 文档字符串
- file-upload - 剑道文件上传-添加文件描述
- angular - 启动模板驱动的表单/表单的对象打字稿Angular 6
- list - 确定第一个列表的所有成员是否都是第二个列表的成员
- lumen - 在流明 5.6 中的响应上设置 cookie
- python - Django如何加入两个查询集,以便它们被顺序序列化
- java - 如何使用 Jayway JsonPath 的嵌套列表展平 json?
- vue.js - Vuex - 传递给操作的参数丢失。替换为当前状态
- c++ - 如何读取 C++ 和 VTK 在 Fortran 中生成的二进制结构化点数据文件?
- dictionary - RavenDB 查询与特定日期范围的父项和最后一个子项的投影