Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The problem is that I have created a "Node" class inherit from Microsoft.VisualBasic.PowerPacks.OvalShape. I added a timer "nodeExpiredTimer" in this class.

C#
class Node: OvalShape
    {
        private int _nodeID;
        private string _nodeName;
        private DateTime _nodeLastMessage;
        private Timer _nodeExpiredTimer;
        
        public Node()
        {       
            _nodeID = -1;
            _nodeName = String.Empty;
            _nodeLastMessage = new DateTime(1, 1, 1, 0, 0, 0);
            _nodeExpiredTimer = new Timer();
	    _nodeExpiredTimer.Interval = 10000;
            _nodeExpiredTimer.Enabled = false;
        }

        public int nodeID
        {
            get { return _nodeID; }
            set { _nodeID = value; }
        }
        
        public string nodeName
        {
            get { return _nodeName; }
            set { _nodeName = value; }
        }

        public DateTime nodeLastMessage
        {
            get { return _nodeLastMessage; }
            set { _nodeLastMessage = value; }
        }

        public Timer nodeExpiredTimer
        {
            get { return _nodeExpiredTimer; }
            set { _nodeExpiredTimer = value; }
        }
     }


In the main thread, I created an array of Node so each node has its timer to determine its Expired time. I want to create a common handler for all timers but I can not identify which node fire the event. I also try using sender but it only help me find which timer fire the event, not the node. Is there any methods to find the node from the identified timer?
Posted
Updated 5-Dec-11 21:56pm
v3

1 solution

Use the Tag property:
C#
public Node()
{
    _nodeID = -1;
    _nodeName = String.Empty;
    _nodeLastMessage = new DateTime(1, 1, 1, 0, 0, 0);
    _nodeExpiredTimer = new Timer();
    _nodeExpiredTimer.Tag = this;         // Add this line.
    _nodeExpiredTimer.Interval = 10000;
    _nodeExpiredTimer.Enabled = false;
}

You can then use the sender property parameter to backtrack to the node:
C#
Timer t = sender as Timer;
if (t != null)
    {
    Node n = t.Tag as Node;
    if (n != null)
        {
        ...
        }
    }


[edit]"property" replaced with "parameter" - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
Duy Dam 6-Dec-11 4:23am    
Thank you for your quick reply!
It works perfectly fine :D
Michel [mjbohn] 6-Dec-11 5:49am    
my 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900