CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2005
    Posts
    135

    can't pass parameter to method?

    I'm using a timer but can't pass a parameter in ProcessDir.
    The error is method name expected.

    timer.Elapsed += new ElapsedEventHandler(ProcessDir("C:\\test\\"));
    timer.Interval = 5000;
    timer.Enabled = true;

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

    Re: can't pass parameter to method?

    Anytime you add an event handler, you need to add it according to the event delegate's signature. See ElapsedEventDelegate in msdn for a code snippet.

    Here's a snippet from the docs (notice how the OnTimeEvent method is declared and called?).

    Code:
    public class Timer1
    {
     
         public static void Main()
         {
             System.Timers.Timer aTimer = new System.Timers.Timer();
             aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
             // Set the Interval to 5 seconds.
             aTimer.Interval=5000;
             aTimer.Enabled=true;
     
             Console.WriteLine("Press \'q\' to quit the sample.");
             while(Console.Read()!='q');
         }
     
         // Specify what you want to happen when the Elapsed event is raised.
         private static void OnTimedEvent(object source, ElapsedEventArgs e)
         {
             Console.WriteLine("Hello World!");
         }
    }

  3. #3
    Join Date
    Oct 2007
    Location
    Alaska
    Posts
    62

    Re: can't pass parameter to method?

    If you read MSDN you'll notice that you are confusing the meaning of the first parameter. It does not mean that you can pass an object as you are trying to do. It is a reference to the timer object that threw the event.

    Open up your copy of Visual Studio then under Help | Search, enter "System.Timers.Timer.ElapsedEventHandler" and click search. You will find the following:

    Code:
    public delegate void ElapsedEventHandler (
    	Object sender,
    	ElapsedEventArgs e
    )
    If you would provide more information as to what you are trying to do. Someone may be able to help you find another solution.
    Microsoft Framework Programmer
    http://kensino.com

  4. #4
    Join Date
    May 2019
    Posts
    2

    Re: can't pass parameter to method?

    Quote Originally Posted by Arjay View Post
    Anytime you add an event handler, you need to add it according to the event delegate's signature. See ElapsedEventDelegate in msdn for a code snippet.

    Here's a snippet from the docs (notice how the OnTimeEvent method is declared and called?).

    Code:
    public class Timer1
    {
     
         public static void Main()
         {
             System.Timers.Timer aTimer = new System.Timers.Timer();
             aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
             // Set the Interval to 5 seconds.
             aTimer.Interval=5000;
             aTimer.Enabled=true;
     
             Console.WriteLine("Press \'q\' to quit the sample.");
             while(Console.Read()!='q');
         }
     
         // Specify what you want to happen when the Elapsed event is raised.
         private static void OnTimedEvent(object source, ElapsedEventArgs e)
         {
             Console.WriteLine("Hello World!");
         }
    }
    You didn't solve his problem. Can you solve it ?

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

    Re: can't pass parameter to method?

    Quote Originally Posted by thepashupatee View Post
    You didn't solve his problem. Can you solve it ?
    I got the OP pointed in the correct direction. Had the OP posted back 12 years ago with additional questions, I would have instructed him with how to set up the code in order to be able to extract the path info from the source parameter.
    Last edited by Arjay; May 23rd, 2019 at 05:05 PM.

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