1

我对 Windows 编程非常陌生,我有一个 Windows 应用程序,单击按钮时,它会调用 Web 服务并从 Web 服务获取数据,然后将值插入数据库。我需要显示所有这一切的进度条。下面是我的代码

private void btnService_Click(object sender, EventArgs e)
{
    //call to the web service
        //get the data
        //insert the returned data from web service to the database.   
}

我将进度条控件放在我的页面上,但在我看来,我必须为进度条分配一些数字,以便它显示状态。

4

1 回答 1

1

如果您使用的是块进度条,则可以像这样更新其状态

int totalSteps = 10;
for (int i= 1; i<= totalSteps; i++)
{
    //  One chunk of your code

    int progress = i * 100 / totalSteps;
    blocksProgressBar.Value = progress;
}
blocksProgressBar.Value = 0;

如果您使用后台工作者,栏可能会像这样更新

private void btnService_Click(object sender, EventArgs e)
{
    backgroundWorker.RunWorkerAsync();
}

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    int totalSteps = 10;

    for (int i = 1; i <= totalSteps; i++)
    {
        //  One chunk of your code

        int progress = i * 100 / totalSteps;
        backgroundWorker.ReportProgress(progress);
    }
}

private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    blocksProgressBar.Value = e.ProgressPercentage;
}

private void backgroundWorker_Completed(object sender, RunWorkerCompletedEventArgs e)
{
    blocksProgressBar.Value = 0;
}

来源

于 2012-12-21T18:33:58.840 回答