C# 更新后台工作器 winform 中的标签文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15759688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update label text in background worker winforms
提问by Rajeev Kumar
I am using BackGroundWorker class to insert some values in sqlserver. I have for loop here to insert values. i am using following code
我正在使用 BackGroundWorker 类在 sqlserver 中插入一些值。我在这里有 for 循环来插入值。我正在使用以下代码
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.
}
}
How can i update the Label1 text here
我如何在这里更新 Label1 文本
采纳答案by jordanhill123
This should work to change the GUI from a background thread.
这应该可以从后台线程更改 GUI。
Label1.Invoke((MethodInvoker)delegate {
Label1.Text = i.ToString() + "Files Converted";});
回答by Steve
You can't access UI interface objects like a label inside a DoWork method.
The DoWork is running on a different thread than the UI elements.
You need to update your interface through the ProgressChanged event or calling a delegate.
您无法访问 DoWork 方法中的标签等 UI 界面对象。
DoWork 与 UI 元素在不同的线程上运行。
您需要通过 ProgressChanged 事件或调用委托来更新您的界面。
First set the WorkerReportsProgress
property of the BackgroundWorker to True,
then, the call to ReportProgress
method, will raise the event ProgressChanged
that will be run in the same thread of your interface elements
首先将WorkerReportsProgress
BackgroundWorker的属性设置为 True,然后调用ReportProgress
方法,将引发ProgressChanged
将在界面元素的同一线程中运行的事件
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Label1.Text = e.ProgressPercentage.ToString();
}
回答by Tomtom
You have to implement the ProgressChanged-Event.
您必须实现 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;
}
回答by Mr.M668
//You can also try this update your label
//你也可以试试这个更新你的标签
this.Invoke(new MethodInvoker(delegate
{
Label1.Text = i.ToString() + "Files Converted";
}));
回答by Anthony Griggs
I was having the same issue. I was already within the Progress Changed Background Worker Event but even using both versions of the Method Invoker above did not help. Then I tried this and it worked:
我遇到了同样的问题。我已经在 Progress Changed Background Worker Event 中了,但即使使用上面的 Method Invoker 的两个版本也无济于事。然后我尝试了这个并且它起作用了:
lblCount.Text = string.Format("Total Directories: {0} Total Files: {1}", TotalDirectories, TotalFiles);
lblCount.Update();