mitchellt
mitchellt

Reputation: 1012

"Method Name Expected" when setting up event handler

I am trying to create a timer for a Project class (multiple instances) which when elapsed will call a static method from another class (BuildEngine.AddBuild) to add itself (the project) to a build queue.

I get the following error:

Error   4   Method name expected

Build Engine Class:

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

Upvotes: 1

Views: 586

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 148980

The signature of the BuildEngine method does not match that of the ElapsedEventHandler delegate, and even if it did, you can't provide parameters to it like that.

Try binding the event to lambda expression (also called an anonymous function) instead:

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

Upvotes: 3

Related Questions