Nimesh
Nimesh

Reputation:

Passing Custom event arguments to timer_TICK event

I have class

//Create GroupFieldArgs as an EventArgs
    public class GroupFieldArgs : EventArgs
    {
        private string groupName = string.Empty;
        private int aggregateValue = 0;
        //Get the fieldName
        public string GroupName
        {
            set { groupName = value; }
            get { return groupName; }
        }
        //Get the aggregate value
        public int AggregateValue
        {
            set { aggregateValue = value; }
            get { return aggregateValue; }
        }
    }

I have another class that creates a event handler

public class Groupby
{
 public event EventHandler eh;
}

Finally I have Timer on my form that has Timer_TICK event. I want to pass GroupFieldArgs in Timer_TICK event. What is the best way to do it?

Upvotes: 0

Views: 8701

Answers (4)

Tim Long
Tim Long

Reputation: 13788

You could subclass the standard timer class and add a property where you can inject your custom event args. Your new class could handle the Elapsed event internally, and expose a new event which would carry your EventArgs.

namespace Test
{
    class MyTimer : System.Timers.Timer
    {
        public EventArgs MyEventArgs { get; set; }

        public MyTimer()
        {
            this.Elapsed += new System.Timers.ElapsedEventHandler(MyTimer_Elapsed);
        }

        void MyTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (MyEventArgs != null)
                OnMyTimerElapsed(MyEventArgs);
            else
                OnMyTimerElapsed(e);
        }

        protected virtual void OnMyTimerElapsed(EventArgs ea)
        {
            if (MyTimerElapsed != null)
            {
                MyTimerElapsed(this, ea);
            }
        }

        public event EventHandler MyTimerElapsed;
    }
}

Upvotes: 1

ufo
ufo

Reputation: 1642

Simply attach your arg(s) to the "Tag" property of your timer object - anything else "might" be overkill:

Timer SomeTimer;
void SomeMethod()
{
    SomeTimer = new Timer();
    SomeTimer.Tag = "myArgString"; // or struct, class, list, etc..
    SomeTimer.Tick += new EventHandler(SomeTimer_Tick);
    SomeTimer.Start();
}

void SomeTimer_Tick(object sender, EventArgs e)
{
    string s = ((Timer)sender).Tag; // SomeTimer.Tag
}

Upvotes: 8

MaxGuernseyIII
MaxGuernseyIII

Reputation:

As Grzenio pointed out, it's always going to generate the same kind of event.

That said, you can create an object that "bounces" the event to its intended recipients...

public class GroupFieldArgSender
{
  public string GroupName  { get; set; }
  public int AggregateValue { get; set; }

  public void RebroadcastEventWithRequiredData(object sender, EventArgs e)
  {
    if (GroupFieldEvent != null)
    {
      GroupFieldEvent(sender, new GroupFieldEvent
        { GroupName = GroupName, AggregateValue = AggregateValue });
    }
  }

  public event EventHandler<GroupFieldEventArgs> GroupFieldEvent;
}

Then wire the timer tick event to the RebroadcastEventWithRequiredData method.

Upvotes: 0

Grzenio
Grzenio

Reputation: 36649

I am not sure what you are trying to achieve. The Timer.Tick event always returns plain EventArgs, because it is driven from within the .Net framework. Do you want to return GroupFieldArgs from the event you defined in Groupby class?

Upvotes: 0

Related Questions