0

I have a simple Backgroundworker and want to write my result to the Console and I also want to report the process.

 class Program
    {
        private static BackgroundWorker worker;
        static int counter;

        static void Main(string[] args)
        {
            worker = new BackgroundWorker();
            worker.WorkerReportsProgress = true;
            worker.RunWorkerAsync();
            Console.ReadLine();
        }

        static void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                counter++;
                worker.ReportProgress(counter);
            }
        }

        static void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            Console.WriteLine(counter);
        }

        static void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }    
    }

But how can I give my String (or more then 1 String) to my ReportProcess Function?

Brody
  • 2,074
  • 1
  • 18
  • 23
  • Change the output in your progress changed. Such as, WriteLine("Current value: {0}",counter) – BugFinder Mar 20 '16 at 01:05
  • i still get no output oO –  Mar 20 '16 at 01:07
  • 1
    Doesnt look like you've assigned the progress changed function to the worker.. – BugFinder Mar 20 '16 at 01:08
  • 1
    What examples on BackgroundWorker have you researched? There are many out there. http://stackoverflow.com/questions/6365887/can-you-link-to-a-good-example-of-using-backgroundworker-without-placing-it-on-a is just one. – Kory Gill Mar 20 '16 at 01:09
  • Its one but not a clearly understanding one :) i dont only want some code i want to understand it ... or to understand how simple give a string to the reportprocess and then write into Console. –  Mar 20 '16 at 01:12
  • so take my hint and see where that takes you – BugFinder Mar 20 '16 at 01:13
  • @BugFinder saw it now lol moment –  Mar 20 '16 at 01:14
  • @BugFinder i dont get it .... i guess i need to go back to School ? –  Mar 20 '16 at 01:18

2 Answers2

2

You're not assigning the event handlers to the BackgroundWorker events:

    static void Main(string[] args)
    {
        worker = new BackgroundWorker();
        worker.DoWork += Worker_DoWork; //here
        worker.ProgressChanged += Worker_ProgressChanged; //and here
        worker.WorkerReportsProgress = true;
        worker.RunWorkerAsync();
        Console.ReadLine();
    }

Cheers

Luc Morin
  • 5,302
  • 20
  • 39
0

You're passing counter as the first (int percentProgress) parameter to ReportProgress, which you can access as shown in the example below using e.ProgressPercentage. You can also pass a second (object userState) parameter, which could be a string or any other object, accessed as shown in the example below using e.UserState.

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    backgroundWorker.ReportProgress(100, "Complete!");
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    toolStripProgressBar.Value = e.ProgressPercentage;
    toolStripStatusLabel.Text = e.UserState as String;
}

See full example at https://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripprogressbar(v=vs.110).aspx

Scott Hutchinson
  • 1,703
  • 12
  • 22
  • static void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) { Console.WriteLine(e.UserState as String); } –  Mar 20 '16 at 01:31
  • Also look at Luc Morin's answer below, as well as the full example at the link (if needed). – Scott Hutchinson Mar 20 '16 at 01:32