-
October 1st, 2007, 10:57 AM
#1
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;
-
October 1st, 2007, 11:12 AM
#2
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!");
}
}
-
October 2nd, 2007, 03:27 AM
#3
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.
-
May 23rd, 2019, 01:49 PM
#4
Re: can't pass parameter to method?
Originally Posted by Arjay
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 ?
-
May 23rd, 2019, 05:03 PM
#5
Re: can't pass parameter to method?
Originally Posted by thepashupatee
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|