BornToGrill
BornToGrill

Reputation: 60

Passing a variable to timer_tick

Got a problem at the moment, I'm using a timer to do animations and I want to be able to decide where to start using Start and Stop integers as i'll show below.

private void Button1_Click(object sender, EventArgs e)
{
    AnimateKey(0,100); 
}

private void AnimateKey(int Start, int Stop)
{
    myTimer.Interval = 5;
    myTimer.Tick += new EventHandler(myTimer_Tick);
    myTimer.Enabled = true;
    myTimer.Start();
}

private void myTimer_Tick(object sender, EventArgs e)
{
    lock (myTimer)
    {
        int StartingFrame = Start;
        int StopFrame = Stop;
        etc...etc..
    }
}

Now my problem is that I want to pass the values 0 and 100 to the Timer Tick event but I have no idea on how to go about doing it. How can I get the Integers 0 and 100 from the button click to the timer tick ?

Upvotes: 0

Views: 2123

Answers (3)

Servy
Servy

Reputation: 203842

Just use a lambda when defining the tick event handler to close over the parameters you need:

private void AnimateKey(int Start, int Stop)
{
    myTimer.Interval = 5;
    myTimer.Tick += (s, args) => myTimer_Tick(Start, Stop);
    myTimer.Enabled = true;
    myTimer.Start();
}

private void myTimer_Tick(int Start, int Stop)
{
    //Do stuff
}

Also note that the Tick event of the Timer that you're using will be fired in the UI thread, so there is no need for a lock; the code is already synchronized.

Upvotes: 5

A. Abramov
A. Abramov

Reputation: 1865

Somewhat hard to understand what you mean, but let's give it a try. If you ment you want to pass the integers start and stop to the function TimerTick, you probably dont understand the EventArgs parameter. EventArgs is ment to store the arguements which are relevant to your scenario - and the solution is simple.

class myTimerEventArgs:EventArgs // Declaring your own event arguements which you want to send
{
 public int start{get;set;} 
 public int stop {get;set;}
 /*Constructor, etc...*/
}
...
//Making the call inside another class:
myTimer_Tick(this,new myTimerEventArgs(0,100);

However, I could be misunderstanding you; If are talking about counting the ticks until it reaches 100 ticks (/intervals), the solution is a simple function added to the event, which would probably look like this:

int Count = 0;
...
private void Counter(object sender, EventArgs e)
{
Count++;
}
...
private void AnimateKey(int Start, int Stop)
 {
     myTimer.Interval = 5;
     myTimer.Tick += new EventHandler(myTimer_Tick);
     myTimer.Tick += new EventHandler(Counter);
     myTimer.Enabled = true;
     myTimer.Start();
     while(Count!=100);
     myTimer.Stop();
 }

Hope I helped, have a nice day :)

Upvotes: 0

DrKoch
DrKoch

Reputation: 9772

use a class with all info:

public class TimerInfo
{
     public int Start;
     public int Stop;
}

store an instance in timer's Tag

myTimer.Tag = new TimerInfo { Start = 0, Stop = 100 };

inside the eventhandler you access this info

myTimer = (Timer)sender;
TimerInfo ti = (TimerInfo)myTimer.Tag;

Upvotes: 0

Related Questions