Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#backgroundWorker报告字符串?_C#_Visual Studio 2008_Reporting_Backgroundworker_Progress - Fatal编程技术网

C#backgroundWorker报告字符串?

C#backgroundWorker报告字符串?,c#,visual-studio-2008,reporting,backgroundworker,progress,C#,Visual Studio 2008,Reporting,Backgroundworker,Progress,如何从backgroundWorker向我的windows.form报告字符串(如“正在搜索文件…”、“找到所选内容…”)以及百分比。此外,我还有一个大型类,其中包含我希望在backgroundWorker_工作中运行的方法。我可以通过Class_method()调用它;但是我无法从被调用的类报告我的完成百分比或任何内容,只能从backgroundWorker_工作方法报告 谢谢 使用代理。阅读 这是一个由三部分组成的系列 您可以使用ReportProgress方法的userState参数来报告

如何从backgroundWorker向我的windows.form报告字符串(如“正在搜索文件…”、“找到所选内容…”)以及百分比。此外,我还有一个大型类,其中包含我希望在backgroundWorker_工作中运行的方法。我可以通过Class_method()调用它;但是我无法从被调用的类报告我的完成百分比或任何内容,只能从backgroundWorker_工作方法报告


谢谢

使用代理。

阅读


这是一个由三部分组成的系列

您可以使用
ReportProgress
方法的userState参数来报告该字符串

以下是MSDN中的一个示例:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    // This method will run on a thread other than the UI thread.
    // Be sure not to manipulate any Windows Forms controls created
    // on the UI thread from this method.
    backgroundWorker.ReportProgress(0, "Working...");
    Decimal lastlast = 0;
    Decimal last = 1;
    Decimal current;
    if (requestedCount >= 1)
    { AppendNumber(0); }
    if (requestedCount >= 2)
    { AppendNumber(1); }
    for (int i = 2; i < requestedCount; ++i)
    {
        // Calculate the number.
        checked { current = lastlast + last; }
        // Introduce some delay to simulate a more complicated calculation.
        System.Threading.Thread.Sleep(100);
        AppendNumber(current);
        backgroundWorker.ReportProgress((100 * i) / requestedCount, "Working...");
        // Get ready for the next iteration.
        lastlast = last;
        last = current;
    }

    backgroundWorker.ReportProgress(100, "Complete!");
}
private void backgroundWorker1\u DoWork(对象发送方,DoWorkEventArgs e)
{
//此方法将在UI线程以外的线程上运行。
//确保不要操纵创建的任何Windows窗体控件
//在UI线程上使用此方法。
backgroundWorker.ReportProgress(0,“正在工作…”);
十进制lastlast=0;
小数点后=1;
十进制电流;
如果(requestedCount>=1)
{AppendNumber(0);}
如果(requestedCount>=2)
{AppendNumber(1);}
对于(int i=2;i
我假设WCF也有这个方法

public void ReportProgress(int percentProgress, Object userState); 
所以只需使用userState来报告字符串

private void worker_DoWork(object sender, DoWorkEventArgs e)
{
 //report some progress
 e.ReportProgress(0,"Initiating countdown");

// initate the countdown.
}
您将在ProgressChanged事件中获得“启动倒计时”字符串

private void worker_ProgressChanged(object sender,ProgressChangedEventArgs e) 
{
  statusLabel.Text = e.UserState as String;
}