在C#中的进度条和BackgroundWorker的 [英] progress bar and backgroundworker in C#

查看:148
本文介绍了在C#中的进度条和BackgroundWorker的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在这就需要使用来自Web服务,
大数据量有服务类发送输入日期到服务器,并取回结果,
,由于它耗时一个项目过程中,它需要用户组合进度和BackgroundWorker的显示用户的过程的百分比。我曾经浏览关于这个主题相当多的示例代码,但还是没能弄清楚做到这一点的最好办法。请你帮忙,
我的代码如下,

 私人MyCollection的[] callWebService(字符串[]输入,串法)
{
名单,LT;字符串>结果=新的List<串GT;();
串FIEL dNames =; // TODO - 如果在循环
INT sizeOfArray = 500精光解决这个问题;
的for(int i = 0; I< Inputs.Length; I = I + sizeOfArray)
{
的String [] outputRecords;
INT的errorCode;
线为errorString;
的String [] = thisFiveHundred createSubArray(输入,I,sizeOfArray);


iq.NameValuePair [] namevaluepairs中=新iq.NameValuePair [0];
FIELDNAMES = iqOfficeWebservice.BatchStan(方法,thisFiveHundred,NULL,,出outputRecords,出来的errorCode,出为errorString);
results.AddRange(outputRecords);
}
results.ToArray();
IAddress [] = formattedResults convertStringsToInputs(字段名,成绩);
返回formattedResults;
}


解决方案

 私人无效cmdButton_Click(对象发件人,EventArgs五)
{
BackgroundWorker的工人=新的BackgroundWorker();
worker.WorkerReportsProgress = TRUE;
worker.DoWork + =新DoWorkEventHandler(worker_DoWork);
worker.ProgressChanged + =新ProgressChangedEventHandler(worker_ProgressChanged);
worker.RunWorkerAsync();
}

私人无效worker_DoWork(对象发件人,DoWorkEventArgs E)
{
BackgroundWorker的工人=发件人为BackgroundWorker的;

的for(int i = 0; I< 101;我++)
{
worker.ReportProgress(I)
System.Threading.Thread.Sleep(1000);
}
}

私人无效worker_ProgressChanged(对象发件人,ProgressChangedEventArgs E)
{
lblProgress.Text =(进展:+ e.ProgressPercentage的ToString()+%);
}



附加信息可以发现的这里


Currently I am working on a project which need to consume large amount data from web services, There is service class sending input date to the server and get back the result, due to it time consuming process, it is required to user combined progressbar and backgroundworker to show user the process percentage. I have browsing quite a few sample code on this topic, but still could not figure out the best way to do it. Would you please help, My code is following,

private  MyCollection[] callWebService(string[] Inputs, string method)
{
    List<string> results = new List<string>();
    string fiel dNames = ""; // todo - fix this if nothing left in loop
    int sizeOfArray = 500;
    for (int i = 0; i < Inputs.Length; i = i + sizeOfArray)
    {
        string[] outputRecords;
        int errorCode;
        string errorString;
        string[] thisFiveHundred = createSubArray(Inputs, i, sizeOfArray);


        iq.NameValuePair[] namevaluepairs = new iq.NameValuePair[0];
        fieldNames = iqOfficeWebservice.BatchStan(method, thisFiveHundred, null, "",        out outputRecords, out errorCode, out  errorString);
        results.AddRange(outputRecords);
    }
    results.ToArray();
    IAddress[] formattedResults = convertStringsToInputs(fieldNames, results);
    return formattedResults;
}

解决方案

    private void cmdButton_Click(object sender, EventArgs e)
    {
        BackgroundWorker worker = new BackgroundWorker();
        worker.WorkerReportsProgress = true;
        worker.DoWork += new DoWorkEventHandler(worker_DoWork);
        worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
        worker.RunWorkerAsync();
    }

    private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        for (int i = 0; i < 101; i++)
        {
            worker.ReportProgress(i);
            System.Threading.Thread.Sleep(1000);
        }
    }

    private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        lblProgress.Text = ("Progress: " + e.ProgressPercentage.ToString() + "%");
    }

Additional info can be found here.

这篇关于在C#中的进度条和BackgroundWorker的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆