Background working class and messaging using progress events from another class in C #

So, I have one class that launches a new class in a new background worker, and the background worker sends status messages back using the progresschanged section.

When I try and use this by typing

classname.Dataworker.reportprogress(5)

from a separate class, I get an error that I am using an object before the definition.

In the examples I found, everyone uses the same class and different functions inside this.

It might be a silly easy mistake, but I just can't see it, thanks for any help you can give!

General overview of my code:

//form class

public static BackgroundWorker bw = new BackgroundWorker();

onbuttonclick
{
        installer install = new installer();
        bw.WorkerReportsProgress = true;
        bw.WorkerSupportsCancellation = true;
        bw.DoWork += class2.aFunction;
        bw.ProgressChanged += new ProgressChangedEventHandler(mainForm_InstallerEvent);
        bw.RunWorkerAsync();
}

private void mainForm_InstallerEvent(object sender, ProgressChangedEventArgs e)
{

        lbl.Text = e.UserState.ToString();
}

//// class2 class of the working class

aFunction
{
        InstallerForm.bw.ReportProgress(5); //errors on this!
}
+1
source share
4 answers

, , , , , , :   BackgroundWorker worker = (BackgroundWorker) ;

,   worker.reportprogress(..)

, ,: http://www.nerdparadise.com/tech/coding/csharp/backgroundworker/

, , :)

+1

ReportProgress UserState, - :

lbl.Text = e.UserState.ToString();

:

aFunction
{
        InstallerForm.bw.ReportProgress(5, "5% Complete");
}

, e.UserState , ToString() .
- , UserState - .

+6

ReportProgress (-) Progress_Changed. , .

0

This was my lazy workaround (because I did not want to use an extra event handler). At that time, I also did not want to understand userstate;) Therefore, I used a list with all warnings / messages for a certain long operation. Message lines were saved App.Properties.Settingsin the application’s powerful resource repository. Since ReportProgresstakes an integer, I am posting the index within the index ReportProgressup Progress_Changed.

Example: The following method was called in do_work.

 private void LongOperation()
 {
        try
        {
          //the operation
          if (success){
             //write a message to a status label
             bgWorker.ReportProgress(1);
          }
          else{
             //write a message to a status label
             bgWorker.ReportProgress(2); 
          }              
        }
        catch(){...}
  }

  public void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs p)
  {
      int lstIndex = p.ProgressPercentage;
      lblStatus.Text = mssglist[lstIndex].ToString(); 
  }
0
source

Source: https://habr.com/ru/post/1727190/


All Articles