简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM