事件处理程序& C#中的后台工作程序错误

时间:2012-09-23 19:56:39

标签: c# class backgroundworker visual-studio-2012

我对C#有点新意,我正在编写概念证明客户端/服务器应用程序。现在我正在尝试使用后台工作程序来更新类中的列表框。这样我就可以提供基本的日志记录。我知道只使用列表框并不是最佳选择。

我已根据以下代码向我的类添加了一个事件处理程序: BackgroundWorker report progress from external Class?

但是我收到以下错误: 无法分配到“ReportProgress”,因为它是“方法组”

我试图减少我的代码只显示相关部分。另外我还没有完全实现上一课的代码,我只是想知道我错过了导致错误的内容。我觉得这可能很简单。

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; }
    }
}

3 个答案:

答案 0 :(得分:2)

ReportProgress是一种方法。

您正在尝试为 ProgressReported 事件添加处理程序。

答案 1 :(得分:0)

我觉得你很困惑。 ReportProgress不是事件委托,而是方法。 看看BackgroundWorker.ProgressChanged Event。我认为这是你正在寻找的处理程序。

答案 2 :(得分:0)

看看这是否提供了您需要的线索......

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;

 namespace WindowsFormsApplication2
 {
    public partial class Form3 : Form
    {
        Server server;

        public Form3()
        {
            InitializeComponent();

            server = new Server();

            BackgroundWorker wrkr = new BackgroundWorker();
            wrkr.WorkerReportsProgress = true;
            wrkr.RunWorkerCompleted += new RunWorkerCompletedEventHandler(wrkr_RunWorkerCompleted);
            wrkr.ProgressChanged += new ProgressChangedEventHandler(wrkr_ProgressChanged);
            wrkr.DoWork += new DoWorkEventHandler(wrkr_DoWork);
        }

        void wrkr_DoWork(object sender, DoWorkEventArgs e)
        {
            // do work...perhaps you might be incrementing a progress meter...
            for (int i = 0; i < 100; i++)
            {
                ((BackgroundWorker)sender).ReportProgress(i,"Some message...");
            }

            e.Result = "some result...";
        }

        void wrkr_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // here you can use e.ProgressPercentage and e.UserState
            // to change the state of something on the thread the worker was created on...
            server.Percentage = e.ProgressPercentage;
            server.Message = e.UserState.ToString();
        }

        void wrkr_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // here you can do some final stuff. clean-up etc.
            server.Percentage = 100;
            server.Message = e.Result.ToString();
        }
    }
}