本文介绍了如何将函数传递给过载backgroundWorker1.RunWorkerAsync(?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:Visual Studio 2010(C#),SQL Server 2012,DB2

项目类型:Windows窗格



问题描述:

我有一个耗时的从SQL Server db获取数据的过程:



private void getAllPartsByDrawingNo(string sValue),没有返回值

或者它可能是:

private int getAllPartsByDrawingNoFunc(string sValue)返回1.



我会喜欢使用BackgroundWorker和ProgressBar来显示进度(参见下面的代码)。



问题:我明白我必须在RunWorkerAsync(???)重载中以某种方式调用上面的函数但我无法弄清楚如何做到这一点,或者我可能完全错了吗?如果可能的话给我一个很好的例子



代码:

private void btnStart2_Click(object sender,EventArgs e)

{

btnStart2.Enabled = false;

btnCancel2.Enabled = true;



backgroundWorker1.RunWorkerAsync( ???);

}



private void backgroundWorker1_DoWork(object sender,DoWorkEventArgs e)

{

for(int i = 0; i< 100; i ++)

{

Thread.Sleep(100);



if(i%5 == 0)

{

backgroundWorker1.ReportProgress(i);

}



if(backgroundWorker1.CancellationPending)

{

e.Cancel = true;

backgroundWorker1.ReportProgress(0);

返回;

}

}

backgroundWorker1.ReportProgress(100) ;

}

...到目前为止..

Environment:Visual Studio 2010 (C#), SQL Server 2012, DB2
Type of Project:Windows Forms

Problem description:
I have a time-consuming process for getting data from SQL Server db:

private void getAllPartsByDrawingNo(string sValue) with no returning value
or it could be:
private int getAllPartsByDrawingNoFunc(string sValue) which return 1.

I would like to use BackgroundWorker and ProgressBar to show the progress (see code below).

Question:I understand that I have to call above function somehow in RunWorkerAsync(???) overload but I cannot figure out how to do that, or maybe I completely wrong in that? If possible show me a good example

Code:
private void btnStart2_Click(object sender, EventArgs e)
{
btnStart2.Enabled = false;
btnCancel2.Enabled = true;

backgroundWorker1.RunWorkerAsync(???);
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(100);

if (i % 5 == 0)
{
backgroundWorker1.ReportProgress(i);
}

if(backgroundWorker1.CancellationPending)
{
e.Cancel = true;
backgroundWorker1.ReportProgress(0);
return;
}
}
backgroundWorker1.ReportProgress(100);
}
…and so far..

推荐答案


这篇关于如何将函数传递给过载backgroundWorker1.RunWorkerAsync(?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 16:56