Using Timer for two differnt processes

truenorth12lds

New member
Joined
May 23, 2020
Messages
4
Programming Experience
Beginner
Is there a way to use Timer1 for all checkbox I create, while still allowing separate pings as desired?

Current operation:
Checkbox1 is checked, Timer1 is started, EHost1 is read and uses Timer1 to do a consistent ping.
Checkbox2 is checked, Timer2 is started, EHost2 is read and uses Timer2 to do a consistent ping.
Checkbox, Timer, and EHost all have to be duplicated for each separate ping I create and perform.

Desired Result:
Checkbox1 is checked, Timer1 is used, EHost1 is read and uses Timer1 to do a consistent ping.
Checkbox2 is checked, Timer1 is used, EHost2 is read and uses Timer1 to do a consistent ping.


Timer:
        private void timer1_Tick(object sender, EventArgs e)
        {
            Ping ping = new Ping();
            PingReply pingresult = ping.Send((EHost1.Text));
            if (pingresult.Status.ToString() == "Success")
            {
                PingStatus1.Text = "Online!";
            }
            else
            {
                PingStatus1.Text = "Offline";
            }

CheckBox1 & CheckBox2:
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                timer1.Start();
            }
            else
            {
                timer1.Stop();
            }
        }
            
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked == true)
            {
                timer2.Start();
            }
            else
            {
                timer2.Stop();
            }
        }
 
Yes. Build a queue a that has the next ping time and the IP address to ping. As each timer tick happens, compare if the ping time has elapsed. If so, do the ping and then queue another ping.
 
What do you mean by "separate pings". You can do as many different things as you want on the Tick of a single Timer. If you don't mind them all being done on the same schedule then things are simple. Just have some sort of list that describes the items to process and, on each Tick, you loop over that list and process each item. You can add and remove items to and from that list to start or stop processing those items.

If you want different schedules for different items then things become a bit more complex but still doable. You just need to set the Interval such that the Timer Ticks frequently enough for every item and then include some logic to determine whether to process an item on that tick. For instance, let's say that you want each item processed every 10 seconds but you want that to start from when an item is added to the list rather than all items processed on the same 10-second schedule. You could include a counter in each item star defaults to 10 and then decrement it on each Tick. When it reaches 0, reset it to 10 and process the item. If the Timer's Interval is set to 1000 then it will Tick about every second and each item will be processed about every 10 seconds.
 
What do you mean by "separate pings". You can do as many different things as you want on the Tick of a single Timer. If you don't mind them all being done on the same schedule then things are simple. Just have some sort of list that describes the items to process and, on each Tick, you loop over that list and process each item. You can add and remove items to and from that list to start or stop processing those items.

If you want different schedules for different items then things become a bit more complex but still doable. You just need to set the Interval such that the Timer Ticks frequently enough for every item and then include some logic to determine whether to process an item on that tick. For instance, let's say that you want each item processed every 10 seconds but you want that to start from when an item is added to the list rather than all items processed on the same 10-second schedule. You could include a counter in each item star defaults to 10 and then decrement it on each Tick. When it reaches 0, reset it to 10 and process the item. If the Timer's Interval is set to 1000 then it will Tick about every second and each item will be processed about every 10 seconds.
This is good info. After reading this, I might be able to just list the IP’s and before each IP, state if checkbox = checked ping. I’m basically creating multiple ping test for each of my clients IP’s. I need to be able to disable a specific IP as needed (which is controlled by a checkbox) and only the ones with a checkbox checked will ping. Let me see what I can do with this info. Thank you again!
 
As an aside, modern programming best practices recommends keeping your data model, view, and business logic separate from each other. This means not using your UI to store your data model. Your UI should only reflect the state of your data model and let the use interact with the data.

Yes, back in old way of programming Windows (and other GUIs), there was a push to keep the data in the UI (and the reason why Microsoft was pushing this whiz bang control, and that whiz bang control, and there is DevExpress and Telerik are more than willing to sell you controls where you just load in your data into them and forget about it). The main reason for this back then was because memory was expensive. So if at all possible, keep only one copy of data in memory. With the rise of Ruby on Rails and it's attendant MVC approach, and the global drop in memory prices, the rest of the world caught on to the benefits of a cleaner separation of concerns.
 
Why complicate things when you don't have 8 or 32 timer limit anymore?

 
Back
Top Bottom