Search code examples
c#classbackgroundworkervisual-studio-2012

Event handler & background worker error in C#


I'm a bit new to C# and I'm writing a proof of concept client/server application. Right now I'm trying to use a background worker to update a listbox from a class. This way I can provide basic logging. I understand just using a listbox is not optimal.

I've added an eventhandler to my class based on this code: BackgroundWorker report progress from external Class?

However I'm getting the following error: Cannot assign to 'ReportProgress' because it is a 'method group'

I've tried to cut down my code to only show relevant parts. Also I have not completely implemented the code from the previous class, I just want to know what I'm missing that is causing the error. I feel like it's probably something simple.

Form1.cs

namespace V12
{
    public partial class Form1 : Form
    {
    //Background Workers
    private BackgroundWorker serverWorker = new BackgroundWorker();

    public Form1()
    {
        InitializeComponent();
        //Server Backgroundworker
        serverWorker.WorkerReportsProgress = true;
        serverWorker.ReportProgress += new EventHandler<Server.ProgressArgs>(serverWorker_ReportProgress); //Error on this line
        serverWorker.DoWork += new DoWorkEventHandler(serverWorker_DoWork);
    }

private void serverWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        //my work is here

    }
    protected void serverWorker_ReportProgress(object sender, Server.ProgressArgs e)
    {
        serverWorker.ReportProgress(e.Percentage, e.Message);
    }
}

Server.cs

namespace V12
{
public sealed class Server
{
    //Allows for updating of control on the UI Thread
    public EventHandler<ProgressArgs> ReportProgress;
    // Eventargs to contain information to send to the subscriber
    public class ProgressArgs : EventArgs
    {
        public int Percentage { get; set; }
        public object Message { get; set; }
    }
}

Solution

  • I think you confused yourself. ReportProgress is not an event delegate, but a method. Have a look at BackgroundWorker.ProgressChanged Event. I think it is the handler you are looking for.