设置事件处理程序时“预期方法名称”

时间:2013-12-31 20:55:07

标签: c# event-handling

我正在尝试为Project类(多个实例)创建一个计时器,当它过去时,将从另一个类(BuildEngine.AddBuild)调用静态方法,将自身(项目)添加到构建队列。

我收到以下错误:

Error   4   Method name expected

构建引擎类:

        // Set timers for builds
        _Logger.Log("Scheduling Builds ...");
        foreach (Project project in _ProjectList)
        {
            switch (project.TriggerType)
            {
                case "Scheduled":
                    TimeSpan nowTime = new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 0);
                    TimeSpan projectTime = project.Time;
                    project.ProjectTimer = new Timer(nowTime.Subtract(projectTime).TotalMilliseconds);
                    project.ProjectTimer.Elapsed += new ElapsedEventHandler(BuildEngine.AddBuild(project));
                    project.ProjectTimer.AutoReset = true;
                    project.ProjectTimer.Enabled = true;
                    break;
                case "Continuous":

                    break;
            }
        }

BuildEngine.AddBuild():

    public static void AddBuild(Project project)
    {
        Build build = new Build();
        build.Project = project;
        build.BuildNumber = -1;
        build.BuildStatus = BuildStatus.NotBuilding;

        _BuildQueue.Add(build);
    }

1 个答案:

答案 0 :(得分:3)

BuildEngine方法的签名与ElapsedEventHandler委托的签名不一致,即使有,也不能像那样提供参数。

尝试将事件绑定到lambda expression(也称为匿名函数):

project.ProjectTimer.Elapsed += (s, e) => BuildEngine.AddBuild(project);