【发布时间】:2017-01-31 04:39:07
【问题描述】:
当我使用消息框来跟踪程序停止的位置时
在com.CommandText 它什么也没显示,应用程序挂起(冻结)。
如果我在没有Thread 的情况下使用它,同样的功能可以完美运行。
这是什么原因?
public bool load_complete=false;
private void button7_Click(object sender, EventArgs e)
{
Thread t1 = new Thread(new ThreadStart(load_data));
t1.Start();
Thread.Sleep(2000);
while (load_complete == false) ;
t1.Abort();
}
public void load_data()
{
string connectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + Application.StartupPath + "\\Database1.mdf;Integrated Security=True;";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
MessageBox.Show("open");
SqlCommand com = new SqlCommand();
try
{
com.Connection = conn;
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
com.CommandText = "select username,password from login where username ='" + textBox1.Text + "'";
da.SelectCommand = com;
da.Fill(dt);
dataGridView1.DataSource = dt;
username = (string)this.dataGridView1.Rows[0].Cells[0].Value;
pass = (string)this.dataGridView1.Rows[0].Cells[1].Value;
load_complete = true;
}
catch (Exception ex){
MessageBox.Show(ex.Message);
}
}
【问题讨论】:
-
你能用异步任务代替线程吗?
标签: .net multithreading c#-4.0