CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2019
    Posts
    2

    Unable to pass Method as argument to ElapsedEventHandler.

    Following is my Code. I am working on a Windows Service which looks for backup files in a particular folder. If it finds a new one it Picks that up move to an archive folder and Restore the database.

    Since I am not using FileWatcher (i am using DirectoryInfo to check for new files) I need to schedule the windows service to keep checking the folder every 5 minutes.

    Code:
    protected override void OnStart(string[] args){
                        ...
                        ...
                        ...
                        // Some Codes.
                        ...
                        ...
                        ...
                        createOrderTimer = new System.Timers.Timer();
                        createOrderTimer.Elapsed += new ElapsedEventHandler( MoveToProcessed(processed) );
                        createOrderTimer.Interval = 300000; // 15 min
                        createOrderTimer.Enabled = true;
                        createOrderTimer.AutoReset = true;
                        createOrderTimer.Start();
    }
    
    private void MoveToProcessed(string processed){
                        ...
                        ...
                        ...
                        // Some Codes.
                        ...
                        ...
                        ...
    }
    I am getting Error CS0149 - Method name Expected.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Unable to pass Method as argument to ElapsedEventHandler.

    You made the same mistake as the OP in the 12 year old thread. You need to provide the correct event method signature.

    Since about VS2015, Visual Studio makes it easy for you with auto code completion. Simply type:
    Code:
    createOrderTimer.Elapsed +=
    and press the TAB key a couple of times and VS will create the appropriate event handler for you and wire it up to the Elapsed event as well.

    The missing puzzle piece is the object that is passed into the event handler is the 'this' parameter of the class where you have declared the timer. So add a string Path property to this class and inside the handler, cast the object parameter to your class. This allow you to then access the Path property.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured