Tim van Gool
Tim van Gool

Reputation: 305

How to give a value / get a value from a timer

I'm trying to make a bakery simulator using windows froms in c#. i've been told to use a timer instead of thread.sleep but i have no idea how to implement it. What I'm trying to do is assign a random value to an int and when the timer reaches the same value as the int stop the timer and enable a button for a short amount of time.

I have no idea how to begin with this, so any help is much appreciated.

Upvotes: 1

Views: 126

Answers (2)

Einer
Einer

Reputation: 146

You can use the Timer Class from System.Timers. It has an event you can listen to (.Elepsed) and its constructor is called with the milliseconds as an argument. So you can go:

_random=new Random();
_timer=new Timer(_random.Next(1,1000000));
_timer.Elapsed += new ElapsedEventHandler(_notifyUser);
_timer.Enabled = true;

Then your _notifyUser method will be called when time's up.

Upvotes: 1

user2946704
user2946704

Reputation: 314

http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx

This should help! goto the examples section of this page ...

Upvotes: 0

Related Questions