views:

259

answers:

2

I'm porting an application from Java to C# and I'm facing a problem connected to the different behaviors and features between the java.util.Timer and System.Threading.Timer. In fact, in the Java version of the Timer class there's a feature (used in the original code) for scheduling tasks, that is not present in the .NET version, where it is possible to schedule one task (through a delegate function) at a time.

Is there a solution, even a small class-set to implement the feature?

I've noticed the existence of several scheduling libraries (an example is Quartz), but I would prefer a solution which would minimize the amount of code to use.

Thank you all! :)

+1  A: 

There are at least 4 timers in .NET. Your short answer: use System.Threading.Timer. I'd recommend reading about the timers here here, as well as looking into System.Windows.Threading.DispatchTimer (part of WPF) which can dispatch events at particular times, but does so on the UI thread.

Robert Fraser
+1  A: 

You can just use any timer class. For each scheduled task, create a new timer class and just set the Interval property to (ScheduledTime - DateTime.Now).TotalMilliseconds and set the AutoReset property to false (or the Enabeld property to false in the code-behind for the Tick event). That will execute your code once at the specified ScheduledTime.

Allon Guralnek
sometimes I ask questions to StackOverflow like I would do it in real life to a friend: in fact, few minutes after I wrote it here, I elaborated a similar solution to the one you described here... :)
Antonello