更新后台worker winforms中的标签文本

本文关键字:标签 文本 winforms 后台 worker 更新 | 更新日期: 2023-09-27 18:07:05

我使用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.                  
        }    
    }

如何更新这里的Label1文本

更新后台worker winforms中的标签文本

这应该可以从后台线程改变GUI。

Label1.Invoke((MethodInvoker)delegate {
   Label1.Text = i.ToString() + "Files Converted";});

你不能在DoWork方法中访问像标签这样的UI接口对象。
DoWork运行在与UI元素不同的线程上。
您需要通过ProgressChanged事件或调用委托来更新接口。

首先将BackgroundWorker的WorkerReportsProgress属性设置为True,然后,调用ReportProgress方法,将引发事件ProgressChanged,该事件将在接口元素的同一线程中运行

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
     Label1.Text = e.ProgressPercentage.ToString();
}

你必须实现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;
}

//你也可以尝试更新你的标签

this.Invoke(new MethodInvoker(delegate 
          {
             Label1.Text = i.ToString() + "Files Converted";
          }));

我也有同样的问题。我已经在Progress Changed Background Worker事件中了,但即使使用上述方法调用器的两个版本也没有帮助。然后我尝试了这个,它工作了:

 lblCount.Text = string.Format("Total Directories: {0} Total Files: {1}", TotalDirectories, TotalFiles);
 lblCount.Update();