C# backgroundWorker 报告字符串?

发布于 2024-08-02 06:49:27 字数 221 浏览 7 评论 0原文

我怎样才能从backgroundWorker向我的windows.form报告一个字符串(比如“现在搜索文件......”,“找到选择......”)以及百分比。此外,我有一个大类,其中包含我想要在后台Worker_Work 中运行的方法。我可以通过 Class_method(); 调用它但我无法报告我的完成百分比或被调用类中的任何内容,只能从 backgroundWorker_Work 方法中报告。

谢谢!

How can I report a string (like "now searching file. . .", "found selection. . .") back to my windows.form from a backgroundWorker as well as a percentage. Additionally, I have a large class that contains the method I want to run in the backgroundWorker_Work. I can call it by Class_method(); but i am then unable to report my percentage done or anything from the called class, only from the backgroundWorker_Work method.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雅心素梦 2024-08-09 06:49:27

我假设 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 事件中得到“Initiating countdown”字符串

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

I'm assuming WCF also have the method

public void ReportProgress(int percentProgress, Object userState); 

So just use the userState to report the string.

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

// initate the countdown.
}

And you'll get that "Initiating countdown" string back in ProgressChanged event

private void worker_ProgressChanged(object sender,ProgressChangedEventArgs e) 
{
  statusLabel.Text = e.UserState as String;
}
赠佳期 2024-08-09 06:49:27

您可以使用 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!");
}

You can use the userState parameter of ReportProgress method to report that strings.

Here's an example from 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!");
}
铃予 2024-08-09 06:49:27

请阅读Windows 窗体中的简单多线程

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

Read Simple Multi-threading in Windows Forms.

It's a 3-part series.

二货你真萌 2024-08-09 06:49:27

使用委托。

use a delegate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文