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