Background worker class and passing messages using progress events from a different class in c#

假装没事ソ 提交于 2019-12-02 12:34:16

问题


So i have one class which starts a new class in a new background worker, and the background worker passes status messages back using the progresschanged section.

When i try and and use this by typing

classname.Dataworker.reportprogress(5)

from a seperate class i get an error that I am using an object before definition.

The examples I have found all use a single class and different functions within that.

It may be a stupid easy mistake but i just can't see it, thanks for any help you can give!

A general overview of my code is:

//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 the background worker class

aFunction
{
        InstallerForm.bw.ReportProgress(5); //errors on this!
}

回答1:


You will need to call ReportProgress with a UserState if you want to do something like this:

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

Then your call would look something like this:

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

Right now it looks like your e.UserState will be null, and calling ToString() will cause a null reference exception.
Here is an example where the UserState is text.




回答2:


Thanks for the help, between those answers and one I found I've managed to get it work, the line I was missing is: BackgroundWorker worker = (BackgroundWorker)sender;

and then reference that worker object with worker.reportprogress(..)

The guide i found useful is: http://www.nerdparadise.com/tech/coding/csharp/backgroundworker/

perfect, thanks guys :)




回答3:


ReportProgress is used by the background worker thread to pass a percentage value to the Progress_Changed delegate(s). This article shows you examples both synchronous and asynchronous.




回答4:


This was my lazy workaround (because I didn't want to use an extra event handler) so far. At the that time I also didn't want to understand the userstate ;) So I used a list with all the alerts/messages for a particular long operation. Message strings were saved in App.Properties.Settings, the mighty resource repository within the app. Since ReportProgress is taking an integer, I send list index within ReportProgress to Progress_Changed.

example: 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(); 
  }


来源:https://stackoverflow.com/questions/1675124/background-worker-class-and-passing-messages-using-progress-events-from-a-differ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!