0

I am adding a background worker process to my dll to report progress back to the gui. I have the following problem

I am getting the following errror :

Severity Code Description Project File Line Suppression State Error CS0120 An object reference is required for the non-static field, method, or property 'Program.slate_ExportReportProgress(object, SlateExportContext.ProgressArgs)' SlateExportApiTest C:\Projects\fhsslateexport\SlateExportApiTest\Program.cs 24 Active

This is my implementation

//update the progress to the client 

public EventHandler<ProgressArgs> ReportProgress;

// Eventargs to contain information to send to the subscriber
public class ProgressArgs : EventArgs
{
    public int Percentage { get; set; }
    public string Message { get; set; }
}

public void TransferToSlateBackground()
{
    var worker = new BackgroundWorker();
    worker.DoWork += DTOBackGroundWorker;
    worker.ProgressChanged += worker_ProgressChanged;
    worker.RunWorkerCompleted += WorkDone;
    worker.RunWorkerAsync("input");
}

public void DTOBackGroundWorker(object sender, DoWorkEventArgs e)
{
    e.Result = e.Argument.Equals("input");
    DTOCaseObject();
}

private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}

public void WorkDone(object sender, RunWorkerCompletedEventArgs e)
{
    var result = (bool)e.Result;
}

The code that raises the ReportProgress event is:

int step = 100 / CaseAssets.Count();
int i = 0;

// Report progress if somebody is listening (subscribed)
if (ReportProgress != null)
{
    ReportProgress(this, new ProgressArgs { Percentage = i, Message = "Exporting to Slate   " + caseid.ToString() });
}

But it is when I am calling the code from the gui / console app i have the problem.

here i wire my event handler

_slateExport.ReportProgress += slate_ExportReportProgress;

public void slate_ExportReportProgress(object sender, ProgressArgs e)
{
}

But I am getting the error above

Edit 2

I don't believe this is the main problem the problem I am facing is with an event not a property or a form control

Prisoner
  • 1,761
  • 2
  • 20
  • 36
  • Its not a null exception Patric Hofman still requires exception no where in the code does it say word null in the error ! –  Nov 02 '16 at 08:14
  • 6
    Sorry, copied the wrong duplicate. This one is better: http://stackoverflow.com/q/498400/993547. – Patrick Hofman Nov 02 '16 at 08:15
  • The faulting line has nothing to do with BGW or its event handler. Where is the code that initializes `ReportProgress` or the code that raises it? Never mind the *standard* question - *WHY* use BGW when you have `Task.Run` and `Progress` ? – Panagiotis Kanavos Nov 02 '16 at 08:20
  • @PanagiotisKanavos I am only familar with Background worker to be able to report the progress back if you can give me example of using the other be great –  Nov 02 '16 at 08:23
  • @ and have a look at my code it clearly shows the point where i raise it –  Nov 02 '16 at 08:24
  • This has nothing to do with a `BackgroundWorker`, your `_slateExport.ReportProgress += slate_ExportReportProgress;` line is inside a **static method**, and `slate_ExportReportProgress` is an instance method. Making the handler a `static` method will remove the error, but most likely it's the other way around: you should be using instance methods everywhere. – Groo Nov 02 '16 at 08:30
  • Take a look at Task and IProgress! – Sebi Nov 02 '16 at 08:33
  • @Goo thanks for closing as I was trying to explain, *and* provide the Task/async/await/Progress alternative. 10 minutes wasted – Panagiotis Kanavos Nov 02 '16 at 08:34
  • Until someone reopens this, what I was writing: The Program class of a typical console application is a static class. This means that the code that runs the BGW can't access the non-static method 'slate_ExportReportProgress'. You should change the signature to a static method: public static void slate_ExportReportProgress(object sender, ProgressArgs e) It's better to ditch BGW altogether though and use Tasks and `async/await`. A BGW can't execute multiple asynchronous operations, nor does it allow you to compose multiple operations. – Panagiotis Kanavos Nov 02 '16 at 08:34
  • Progress reporting can be handled by the [Progress](https://msdn.microsoft.com/en-us/library/hh193692(v=vs.110).aspx) class. For example, the following method allows you to asynchronously – Panagiotis Kanavos Nov 02 '16 at 08:35
  • @PanagiotisKanavos: it's a duplicate, why would anyone want to reopen it? The dupe explains the issue, my comment explains the issue, your comment explains the issue. Replacing the BGW with async/await is a [different question](http://stackoverflow.com/q/12414601/69809), most likely also a duplicate. – Groo Nov 02 '16 at 08:39
  • `static async Task DownloadSomethingAsync(string[] urls, string path,IProgress progress){ use(var client=new HttpClient()){ foreach(var url in urls){var result=await client.GetStringAsync(url);progress.Report(url)}}}` – Panagiotis Kanavos Nov 02 '16 at 08:39
  • @Groo It wasn't a comment, it was part of an answer that I had almost finished. As for the duplicate link, it doesn't explain the actual cause of the problem (because it's defined inside Program, a static class), only the cause for the exception – Panagiotis Kanavos Nov 02 '16 at 08:43
  • @PanagiotisKanavos: first sentence in the duplicate answer: *"It looks like you are calling a non static property from a static method"*. Which is what I wrote too. Which is what you wrote too. OP could call your `DownloadSomethingAsync` method from a static method with `new Progress(t => _label.Text = t)`, and get the exact same compile error, it doesn't solve their problem at all. – Groo Nov 02 '16 at 09:01
  • @Groo no, I wrote 'The Program class of a typical console application is a static class. `. That's the root cause. – Panagiotis Kanavos Nov 02 '16 at 09:04
  • @Groo i have been busy but yes your answer was the correct one when others try to help people in comments when a question is closed I will never understand many thanks. –  Nov 03 '16 at 13:30

0 Answers0