C# backgroundWorker reports string?

后端 未结 4 1705
[愿得一人]
[愿得一人] 2021-01-11 16:24

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,

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-11 17:05

    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!");
    }
    

提交回复
热议问题