gpt4 book ai didi

c# - C#中的进度条和backgroundworker

转载 作者:太空宇宙 更新时间:2023-11-03 17:43:25 24 4
gpt4 key购买 nike

目前,我正在从事一个项目,该项目需要使用Web服务中的大量数据,
服务类将输入日期发送到服务器并获取结果,
由于该过程很耗时,因此需要用户结合进度条和后台工作人员才能向用户显示该过程的百分比。我已经浏览了很多有关此主题的示例代码,但是仍然找不到最佳的方法。请你帮忙,
我的代码如下,

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() + "%");
}

其他信息可以在 here中找到。

关于c# - C#中的进度条和backgroundworker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11623923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com