我正在使用BackGroundWorker类在sqlserver中插入一些值。我在这里循环插入值。我正在使用以下代码
public void bw_Convert_DoWork(object sender, DoWorkEventArgs e)
{
e.Result = e.Argument;
for (int i = 0; i < fTable.Rows.Count; i++)
{
try
{
SqlCommand cmd = new SqlCommand("INSERT INTO TBL_CDR_ANALYZER (LNG_UPLOAD_ID, DAT_START, LNG_DURATION, INT_DIRECTION, INT_CALL_DATA_TYPE, \n" +
"TXT_TARGET_NUMBER, TXT_OTHER_PARTY_NUMBER, TXT_TARGET_IMSI, TXT_TARGET_IMEI, TXT_TARGET_CELL_ID, TXT_ROAMING_NETWORK_COMPANY_NAME) VALUES \n" +
"(@UPLOAD_ID, @START_DATE, @DURATION, @DIRECTION, @CALL_TYPE, @TARGET_NUMBER, @OTHER_PARTY_NUMBER, @IMSI, @IMEI, @CELL_ID, @ROAMING_NAME)", sqlCon);
cmd.Parameters.Add("@UPLOAD_ID", SqlDbType.Int).Value = 1;
cmd.Parameters.Add("@START_DATE", SqlDbType.DateTime).Value = fTable.Rows[i]["CallDate"];
cmd.Parameters.Add("@DURATION", SqlDbType.Int).Value = fTable.Rows[i]["CallDuration"];
cmd.Parameters.Add("@DIRECTION", SqlDbType.Int).Value = GetCallDirection(fTable.Rows[i]["CallDirection"].ToString());
cmd.Parameters.Add("@CALL_TYPE", SqlDbType.Int).Value = GetCallType(fTable.Rows[i]["CallType"].ToString());
cmd.Parameters.Add("@TARGET_NUMBER", SqlDbType.VarChar, 25).Value = fTable.Rows[i]["TargetNo"];
cmd.Parameters.Add("@OTHER_PARTY_NUMBER", SqlDbType.VarChar, 25).Value = fTable.Rows[i]["OtherPartyNo"];
cmd.Parameters.Add("@IMSI", SqlDbType.VarChar, 50).Value = fTable.Rows[i]["IMSI"];
cmd.Parameters.Add("@IMEI", SqlDbType.VarChar, 50).Value = fTable.Rows[i]["IMEI"];
cmd.Parameters.Add("@CELL_ID", SqlDbType.VarChar, 50).Value = fTable.Rows[i]["CellID"];
cmd.Parameters.Add("@ROAMING_NAME", SqlDbType.NVarChar, 255).Value = fTable.Rows[i]["RoamingCompany"];
sqlCon.Open();
cmd.ExecuteNonQuery();
sqlCon.Close();
}
catch (SqlException ex)
{
}
finally
{
sqlCon.Close();
}
bw_Convert.ReportProgress((100 * i) / fTable.Rows.Count);
**Label1.Text = i.ToString() + "Files Converted";** // getting error Here.
}
}
如何更新Label1文本
答案 0 :(得分:30)
这应该可以从后台线程更改GUI。
Label1.Invoke((MethodInvoker)delegate {
Label1.Text = i.ToString() + "Files Converted";});
答案 1 :(得分:7)
您无法像DoWork方法中的标签那样访问UI界面对象 DoWork在与UI元素不同的线程上运行 您需要通过ProgressChanged事件更新界面或调用委托。
首先将BackgroundWorker的WorkerReportsProgress
属性设置为True,
然后,对ReportProgress
方法的调用将引发将在您的界面元素的同一个线程中运行的事件ProgressChanged
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Label1.Text = e.ProgressPercentage.ToString();
}
答案 2 :(得分:3)
您必须实施ProgressChanged-Event。
private void bw_Convert_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//The progress in percentage
int progress = e.ProgressPercentage;
//A custom-value you can pass by calling ReportProgress in DoWork
object obj = e.UserState;
}
答案 3 :(得分:2)
//您也可以尝试更新标签
this.Invoke(new MethodInvoker(delegate
{
Label1.Text = i.ToString() + "Files Converted";
}));
答案 4 :(得分:0)
我遇到了同样的问题。我已经在“更改进度的后台工作人员事件”中,但是即使同时使用上述两个版本的“方法调用程序”也无济于事。然后我尝试了一下,它就起作用了:
public enum Genders { male, female, other }
class student
{
public string Name;
public int Age;
public Genders Gender;
public void Write()
{
Console.WriteLine("Name: {0}, Age: {1}, Gender: {2}", Name, Age, Gender.ToString());
}
}
class Program
{
static void Main(string[] args)
{
student student1 = new student();
student1.Name = "Dan";
student1.Age = 15;
student1.Gender = Genders.male;
student1.Write();
}
}