mercredi 22 décembre 2021

Design pattern for implementing the Polling approach in c#

I want to implement polling approach in c# using design patterns with some generic code with the below scenario.

I have 3 console application names Employee, Orders, Salary and in this from the Employee application I am initiating the Orders and Salary console application , once I will initiate application Orders and Salary console application are long running application which will keep on running and we are updating the local text file with the completion status for each of the application.

From the Employee application I have created a Timers objected and using Elapsed Event for 2 sec to check the file text whether it has status of both the file has been completed or not.

So I want to make the below implementation in more generic way or using some other approach/design patterns so that we can achieve the similar polling concept as I have ore complex situation where I have to use the same repetitive code for each time . Any suggestion on this ?

var poolingTimer = new Timer(2 * 1000);
                        poolingTimer.AutoReset = false;
                        poolingTimer.Elapsed += async delegate
                        {
                            
                            if (logic to check if File text has console application completed status)
                            {
                                poolingTimer.Stop();    
                            }
                            else
                            {
                                poolingTimer.Start();
                            }
                        };

                        poolingTimer.Start();

Aucun commentaire:

Enregistrer un commentaire