如何在执行方法时显示进度条

时间:2014-08-19 11:20:29

标签: c# winforms

    private bool ImportData()
    {
        bool result = false;
        bgw.ReportProgress(1);
        try
        {
            bgw.ReportProgress(3);
            intdevid = int.Parse(cmbDeviceName.SelectedValue.ToString());
            FetchDevicedata(intdevid);  // Fetch Remove Device Info from SQL database

            FetchMaxReportId();
            GetFTPFile(strDeviceIP, strDeviceUsername, strDevicePwd, strDevicePath + "//RunningBatch//RunningBatch.db", "RunningBatch.db"); // Copy RunningBatch.db to Debug Folder from Remote 
            LoadRunningData(); // Get Running Data in dataset from running.db
            if (DecodeBatchData_R() == false)
            {
                MessageBox.Show("Running Batch Data Not Found");
            }// save in batch master and row data table
            bgw.ReportProgress(5);
            GetFTPFile(strDeviceIP, strDeviceUsername, strDevicePwd, strDevicePath + "//CompletedBatch//CompletedBatch.db", "CompletedBatch.db");
            LoadCompletedData();
            if (DecodeBatchData() == false)
            {
                MessageBox.Show("Completed Batch Data not found");
            }
            result = true;
            bgw.ReportProgress(7);
        }
        catch (Exception ex)
        {
            clsLogs.LogError("Error: " + ex.Message + this.Name + " || ImportData");
            result = false;
        }
        return result;
    };

按钮点击事件代码

    private void btnimport_Click(object sender, EventArgs e)
    {
        bgw.RunWorkerAsync();

        progressBar1.Visible = true;
        label2.Visible = true;        
        dsCheckRptId = new DataSet();

        if (cmbDeviceName.Text.ToString().Trim() == "--Select--")
        {
           MessageBox.Show("Please Select Proper Device");
           cmbDeviceName.Focus();
           return;

        }
        if (cmbDeviceName.SelectedValue != null && cmbDeviceName.SelectedValue.ToString().Trim() != "0" && cmbDeviceName.SelectedValue.ToString().Trim() != "System.Data.DataRowView" && cmbDeviceName.SelectedValue.ToString().Trim() != "")
        {
            if (ImportData() == true)
            {
                MessageBox.Show("Data Import Completed Successfully for " + strDevicename);
                clsLogs.LogEvent(3, "Data Import Completed Successfully for " + strDevicename);
            }
            else
            {
                MessageBox.Show("Data Import Fail For " + strDevicename);
                clsLogs.LogEvent(3, "Data Import Fail for " + strDevicename);  
            }    
        }         
    };

背景工作代码

    void bgw_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 1; i <= 100; i++)
        {
            ImportData();
            System.Threading.Thread.Sleep(100);

            bgw.ReportProgress(i);

        }
    }

    void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        label2.Text = String.Format("Progress: {0} %", e.ProgressPercentage);
        label2.Visible = false;
        progressBar1.Visible = false;
    }

    void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {

    }
单击按钮时,

进度条无法打开。请告诉我如何在按钮点击事件中打开进度条。执行importdata()时,会打开进度条,当importdata完成时,进度条应该停止。请给我一些建议,如何打开进度条?

1 个答案:

答案 0 :(得分:2)

每次ProgressChanged发生错误时,您都会设置ProgressBar的Visible 。 这意味着它在UI中不再可见

从ProgressChangedMethod中删除应修复问题的可见更改:

void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
    label2.Text = String.Format("Progress: {0} %", e.ProgressPercentage);
}

然后在BackGroundWorker完成时不显示它:

void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    progressBar1.Visible = false;
    label2.Visible = false;
}

编辑(根据评论....):

如果需要BackgroundWorker的结果,则必须从ImportData返回DoWorkEvents.Result属性:

void bgw_DoWork(object sender, DoWorkEventArgs e)
{
    e.Result = ImportData();
}

然后在完成的方法中处理它:

void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    var result = (bool)e.Result;
    if(result)
    {
        MessageBox.Show("Data Import Completed Successfully for " + strDevicename);
        clsLogs.LogEvent(3, "Data Import Completed Successfully for " + strDevicename);
    }
    else
    {
        MessageBox.Show("Data Import Fail For " + strDevicename);
        clsLogs.LogEvent(3, "Data Import Fail for " + strDevicename);  
    }    
}