Saturday, 29 February 2020

C# - Timer (System.Windows.Forms)


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace LongProcessHandler
{

    public partial class Form1 : Form
    {

        private Timer myTimer = new Timer();
        private bool systemFlag = false;
        private bool processFlag = false;


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myTimer.Tick += new EventHandler(TimerEventProcessor1);
            myTimer.Interval = 5000;
            myTimer.Start();
        }
       
        private void TimerEventProcessor1(Object myObject, EventArgs myEventArgs)
        {
            myTimer.Stop();
            CallProcessor1();           
        }

        private void CallProcessor1()
        {
            Process1();

            if (systemFlag)
            {
                myTimer.Tick += new EventHandler(TimerEventCallProcessor2);
                myTimer.Interval = 5000;
                myTimer.Start();
            }
        }

        private void Process1()
        {
            //Some Processing Here....

            systemFlag = true;           
        }



        private void TimerEventCallProcessor2(Object myObject, EventArgs myEventArgs)
        {
            myTimer.Stop();
            CallProcessor2();

            if (processFlag)
            {
                myTimer.Tick += new EventHandler(TimerEventProcessor2Sub1);
                myTimer.Interval = 5000;
                myTimer.Start();
            }
        }

        private void CallProcessor2()
        {
            Process2();           
        }

        private void Process2()
        {
            //Some Processing Here....
           
            processFlag = true;
        }

        private void TimerEventProcessor2Sub1(Object myObject, EventArgs myEventArgs)
        {
            myTimer.Stop();
            CallProcessor2Sub1();

            if (systemFlag)
            {
                myTimer.Tick += new EventHandler(TimerEventCallProcessor3);
                myTimer.Interval = 5000;
                myTimer.Start();
            }
        }
       
        private void CallProcessor2Sub1()
        {
            Process2Sub1A();
            Process2Sub1B();
        }

        private void Process2Sub1A()
        {
            //Some Processing Here....

            processFlag = true;
        }

        private void Process2Sub1B()
        {
            //Some Processing Here....

            processFlag = true;
        }




        private void TimerEventCallProcessor3(Object myObject, EventArgs myEventArgs)
        {
            CallProcessor3();

            if (systemFlag)
            {               
                EndProcess();
            }
        }

        private void CallProcessor3()
        {
            myTimer.Stop();
            Process3();
        }

        private void Process3()
        {           
            systemFlag = true;
        }


        private void EndProcess()
        {
            //Some Processing Here To End To Whole Process....
        }

    }
}


No comments:

Post a Comment