Create custom monitor

One07k-4914 101 Reputation points
2020-12-09T05:33:11.213+00:00

Hi All,

I have a requirement like I have to monitor for one event for maximum 1 minute if the event comes within that time I can continue. Otherwise make it as failed. This I can do using auto reset event. But I have one more requirement if its in waiting state if user pressed cancel that time I have to cancel it and make it as stopped.
I am not getting an optimal solution.

below is my code snippet.

public Result Start()
{
if (myAutoResetEvent.WaitOne(new TimeSpan(0, 0, 0, 60)))
{

                Done(Result.OK);
            }
          else
           {
             CleanUp();

            }

public Result Stop()
{
//here i have to release the myAutoResetEvent and call the cleanup.
}

private void EventChanged(object sender,EventArgs arg)
{
myAutoResetEvent.Set();
}

Please suggest an optimal solution.

Thanks in advance...

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,922 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Timon Yang-MSFT 9,591 Reputation points
    2020-12-09T09:34:26.96+00:00

    I think this function should be completed by Timer.

    I wrote a simple example, counting down the number.

    If the entire process exceeds the time I set (5 seconds in this example), the process stops, and if I click the stop button, it also stops.

        public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
            }  
            public DateTime StartDateTime { get; set; }  
            private void Form1_Load(object sender, EventArgs e)  
            {  
                timer1.Tick += Timer1_Tick;  
            }  
            int timeLeft = 100;  
            private void Timer1_Tick(object sender, EventArgs e)  
            {  
                TimeSpan duration = new TimeSpan(DateTime.Now.Ticks - StartDateTime.Ticks);  
                Console.WriteLine(duration.TotalSeconds);  
                if (duration.TotalSeconds>5)  
                {  
                    timer1.Stop();  
                }  
                if (timeLeft > 0)  
                {  
                    timeLeft = timeLeft - 1;  
                    textBox1.Text = timeLeft.ToString();  
                }  
                else  
                {  
                    timer1.Stop();  
                    textBox1.Text = "Time's up!";  
                    MessageBox.Show("You didn't finish in time.", "Sorry!");  
                    startBtn.Enabled = true;  
                }  
            }  
            private void startBtn_Click(object sender, EventArgs e)  
            {  
                timer1.Start();  
                StartDateTime = DateTime.Now;  
            }  
            private void stopBtn_Click(object sender, EventArgs e)  
            {  
                timer1.Stop();  
            }  
        }  
    

    Does this logic meet your requirements?


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. One07k-4914 101 Reputation points
    2020-12-09T12:47:18.507+00:00
     WaitHandle[] handle = {myWaitEvent, myStopEvent};
    
                while (true)
                {
                    var evtSignaled = WaitHandle.WaitAny(handle, new TimeSpan(0, 0, WaitTimeOut, 0));//1 minute wait
                    if (evtSignaled == 0)
                        Done(Result.OK);
                    if (evtSignaled == 1)
                        break;
                    if (evtSignaled == WaitHandle.WaitTimeout)
                       CleanUp();
                    break;
    
                }
    

    I found one solution, here i am using 2 auto reset events one for Waiting and other for stop. If wait event triggered within 1 minute will execute in that path. If it is timed out then will execute in the cleanup part. If i pressed stop then i will break from the loop.

    Is there any better solution for this?
    How is this solution?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.