Passing Custom event arguments to timer_TICK event

南楼画角 提交于 2021-02-11 18:16:07

问题


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?


回答1:


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
}



回答2:


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;
    }
}



回答3:


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?




回答4:


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.



来源:https://stackoverflow.com/questions/841359/passing-custom-event-arguments-to-timer-tick-event

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!