EasyTimer – JavaScript style setTimeout and setInterval in C# – Programming Code Examples
C# JavaScript

EasyTimer – JavaScript style setTimeout and setInterval in C#2 min read

I found JavaScript setTimeout and setInterval functions quite handy for timer like functionality and some time wish I could use that in C# too. In an earlier post I create a C# like timer functionality in JavaScript. Now, I want to do opposite i.e. implement JavaScript setTimeout and setInterval like functionality in C#.

This is can be done very easily using Lamda expressions and Timer. Look at the below utility class –

To use setTimeout this you can simply do –

The code will run after 1000 ms delay similarly like JavaScript setTimeout. The function also returns a handle. If you want clearTimeout like functionality, then the simply dispose off the handle.

Similarly you can use setInterval as –

and SetInterval also returns a stop handle which you can use for clearInterval like functionality. Just dispose off the handle –


Leave a Comment