Possible Duplicate:
How to update GUI from another thread in C#?C#线程
我现在有一个C#程序运行一个查询并显示在datagridview
结果。
由于记录大小的查询需要一段时间(20-30秒)才能运行。
我想我会添加一个动画,以便用户至少知道该软件正在运行,并没有停止工作。
当程序正在进行调用时,我无法运行任何东西,因此我查看了线程。
这里是我的代码(原谅我,我还没有真正落实评论):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Threading;
namespace RepSalesNetAnalysis
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pictureBox2.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
GetsalesFigures();
}
private void Form1_Load(object sender, EventArgs e)
{
AutofillAccounts();
}
private void GetsalesFigures()
{
try
{
string myConn = "Server=herp;" +
"Database=shaftdata;" +
"uid=fake;" +
"pwd=faker;" +
"Connect Timeout=120;";
string acct;// test using 1560
SqlConnection conn = new SqlConnection(myConn);
SqlCommand Pareto = new SqlCommand();
BindingSource bindme = new BindingSource();
SqlDataAdapter adapt1 = new SqlDataAdapter(Pareto);
DataSet dataSet1 = new DataSet();
DataTable table1 = new DataTable();
Thread aniSql = new Thread(new ThreadStart(animateIcon));//CREATE THE THREAD
acct = accCollection.Text;
string fromDate = this.dateTimePicker1.Value.ToString("MM/dd/yyyy");
string tooDate = this.dateTimePicker2.Value.ToString("MM/dd/yyyy");
Pareto.Connection = conn;
Pareto.CommandType = CommandType.StoredProcedure;
Pareto.CommandText = "dbo.GetSalesParetotemp";
Pareto.CommandTimeout = 120;
Pareto.Parameters.AddWithValue("@acct", acct);
Pareto.Parameters.AddWithValue("@from", fromDate);
Pareto.Parameters.AddWithValue("@too", tooDate);
aniSql.Start(); //START THE THREAD!
adapt1.Fill(dataSet1, "Pareto");
aniSql.Abort(); //KILL THE THREAD!
//pictureBox2.Visible = false;
this.dataGridView1.AutoGenerateColumns = true;
this.dataGridView1.DataSource = dataSet1;
this.dataGridView1.DataMember = "Pareto";
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCells);
}
catch (Exception execc)
{
MessageBox.Show("Whoops! Seems we couldnt connect to the server!"
+ " information:\n\n" + execc.Message + execc.StackTrace,
"Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
private void AutofillAccounts()
{
//get customers list and fill combo box on form load.
try
{
string myConn1 = "Server=derp;" +
"Database=AutoPart;" +
"uid=fake;" +
"pwd=faker;" +
"Connect Timeout=6000;";
SqlConnection conn1 = new SqlConnection(myConn1);
conn1.Open();
SqlCommand accountFill = new SqlCommand("SELECT keycode FROM dbo.Customer", conn1);
SqlDataReader readacc = accountFill.ExecuteReader();
while (readacc.Read())
{
this.accCollection.Items.Add(readacc.GetString(0).ToString());
}
conn1.Close();
}
catch(Exception exc1)
{
MessageBox.Show("Whoops! Seems we couldnt connect to the server!"
+ " information:\n\n" + exc1.Message + exc1.StackTrace,
"Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
public void animateIcon()
{
// animate
pictureBox2.Visible = true;
}
}
}
正如你可以看到我只想过程调用之前运行的动画,然后就结束吧后。
我的线程知识是全新的。我环顾四周,但目前我有点困惑。
这里是我的错误:
Thrown: "Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on." (System.InvalidOperationException) Exception Message = "Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.", Exception Type = "System.InvalidOperationException"
我需要的,而我的SQL proc已经阅读进行动画的一个非常简单的方法。
类似于picture.visible = true
开始时的状态,当它结束时为false。
当你在Google中输入错误时,你发现了什么? – CodeCaster