?
Solved

Trying to pass a method in as a parameter, using it to create a timer callback

Posted on 2007-08-06
8
Medium Priority
?
249 Views
Last Modified: 2013-12-17
I am creating a simple SafeTimer class, and I would like to be able to create the timer when I instantiate the object.

Here is my instantiation:

public SafeTimer(object pCallbackMethod, int pTimerInterval) {
    lockThreadObject = new object();
    timerProcess = null;
    timerStatus = eStatus.isNull;
   TimerCallback timerProcessCallback = new TimerCallback(pCallbackMethod);
    timerSafe = new SafeTimer(new System.Threading.Timer(timerProcessCallback, null,
                                                                                                     pTimerInterval, pTimerInterval));
    timerInterval = pTimerInterval;
}

Here is how I call it (MyMethod is a void method with an object parameter):
     SafeTimer st = new SafeTimer(MyMethod, 1000);

The error I am getting on this line:
TimerCallback timerProcessCallback = new TimerCallback(pCallbackMethod);

and the error is: "'pCallbackMethod is a variable but is used like a method".

I know that in old school C I could have just used the address of pCallbackMethod, but I couldn't figure out how to do that in .NET.  Is there a way to do what I want, or am I just going to have to create the callback before I instantiate the SafeTimer object?
 
0
Comment
Question by:FrancineTaylor
  • 3
  • 3
  • 2
8 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19640057
Do you have a method called 'pCallbackMethod' that will handle the call back?

Bob
0
 
LVL 1

Author Comment

by:FrancineTaylor
ID: 19640142
This is an example of a class which would use the SafeTimer class which I am trying to create.  I want to keep track of the number of seconds that has passed since the timer was instantiated:

public class UpdateInfo {
   SafeTimer st;
   int counter;

   public UpdateInfo() {
      SafeTimer st = new SafeTimer(MyMethod, 1000);
   }

   private void MyMethod(object pDummy) {
      counter++;
   }
}
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19640186
I see now where 'pCallbackMethod' is defined as an object, and it needs to be a method whose signature is the same as the TimerCallback expects.

Example:

TimerCallback Delegate
http://msdn2.microsoft.com/en-us/library/system.threading.timercallback.aspx

   public void CheckStatus(Object stateInfo)
  {
  }

Bob
0
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
LVL 37

Accepted Solution

by:
gregoryyoung earned 2000 total points
ID: 19640196
public SafeTimer(object pCallbackMethod, int pTimerInterval) {

is your problem try ....

public SafeTimer(TimerCallback pCallbackMethod, int pTimerInterval) {

TimerCallback is a delegate http://msdn2.microsoft.com/en-us/library/system.threading.timercallback.aspx

then create as ..

   public UpdateInfo() {
      SafeTimer st = new SafeTimer(new TimerCallback(MyMethod), 1000);
   }
0
 
LVL 1

Author Comment

by:FrancineTaylor
ID: 19640241
Yes, that actually is my current fall-back coding (I'm not trying to cheat you out of points, Gregory, and if I can't get another answer I will award full points for yours) but I was really hoping to find out if c#.NET has any way to pass in a reference to/address of a method (as opposed to passing in a reference to a delegate).
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 19640274
no it doesn't ... C# 3.0 has lambdas which encapsulate delegates though.

delegates should be considerred type safe function pointers.
0
 
LVL 1

Author Comment

by:FrancineTaylor
ID: 19640296
Okay, thanks...passing in the TimerCallback seems to be as tight as I can get the code, then.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 19640364
an alternative is to use an interface + a small object but I prefer what you have done there.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question