0

This is an old question that I have come back to fix/edit, the essence of the question was how to implement a timer properly which has been answerd many times, so i have marked this as a duplicate

Link to a good answer for implementing a timer: How do you add a timer to a C# console application

JohnChris
  • 1,360
  • 15
  • 29
  • Why a delay? What would be the implementation of that? When a user enters a decimal sign and then quickly removes it, they're going to be notified a second later that their input was at one time incorrect? Use regular validation controls (ErrorProvider) which color your inputs red or something like that, don't use MessageBoxes in the first place. – CodeCaster Sep 13 '16 at 13:44
  • @CodeCaster - the delay is so that if they add it and quickly remove it, they never see the error message. –  Sep 13 '16 at 13:47
  • exactly.... but a color code method does seem like a better idea, but I would still like a message box to pop up if not with a delay then on a counter, lets say every 3 times just to inform the user why its red – JohnChris Sep 13 '16 at 13:49
  • 1
    Just forget the idea of a timer and a MessageBox altogether. Use the out-of-the-box error handling and don't surprise your users with a messagebox when they're typing in a textbox that's different from the one raising the error. – CodeCaster Sep 13 '16 at 13:55

2 Answers2

1

I agree with the @CodeCaster's comment that message box is not the best way to communicate invalid input to the user, but here is how it can be done:

//declare DateTime lastErrMsg = DateTime.MinValue; at your class level
catch (FormatException fEX)
{ 
    if (DateTime.Now - lastErrMsg > TimeSpan.FromSeconds(30)) //or whatever TimeSpan value
    {
        Message.Box("Value must be a divisisable by 1 exactly")
        lastErrMsg = DateTime.Now;
    }
}
slawekwin
  • 6,270
  • 1
  • 44
  • 57
  • thanks for the knowledge, I will try the color coding with a message box, just to fully inform the user – JohnChris Sep 13 '16 at 13:51
  • 1
    @JohnChristophoros maybe add a tooltip to the changed color and you will be all set :) – slawekwin Sep 13 '16 at 13:52
  • 1
    This will only show the messagebox when the validation is triggered again after the timeout. – CodeCaster Sep 13 '16 at 13:54
  • @CodeCaster isn't that the point? besides, OP said he checks this on some kind of mouse event, so probably quite frequently – slawekwin Sep 13 '16 at 13:56
  • 1
    Validating on mouse movement. Sure, forget about keyboard users. The question is flawed to begin with, any solution is going to be suboptimal. – CodeCaster Sep 13 '16 at 13:57
  • @Codecaster, I use the MouseMove event but the method is called whenever a calculation is made. So keyboard users are fine – JohnChris Sep 13 '16 at 14:00
1

Use a Timer. In your catch, do not do the error message, instead, create a Timer object. Add to that timer's elapsed event a handler that will display the message only if the value is still invalid.

catch (FormatException fEX)
{
    if (MyFormatExcTimer == null) {
        MyFormatExcTimer = new Timer(1000);
        MyFormatExcTimer.elapsed += async ( sender, e ) => await HandleTimer();
        MyFormatExcTimer.start();
    }
}

private static Task HandleTimer()
{
    if (... format is still bad ...)
    {
        Message.Box("Value must be a divisisable by 1 exactly");
    } else {
        MyFormatExcTimer.Stop();
    }
    MyFormatExcTimer.Dispose();
    MyFormatExcTimer = null;
}

This is not very complete. You may need to create or dispose or start or stop the timer at different events, but without seeing more of your code it is hard to tell. I might actually not have the program fire off an exception on bad input, but accept any input and have my own code that tests it. My code would get fired off on any text changed event and could set off the timer if the input is bad or stop it if the input is ok. Also, you might consider not even firing off any messages during input, but rather firing verifying the data when the user tries to save it... but that assumes you have a "save" button or something like that, which you may not.

  • This is not maintainable. What if one form can generate multiple errors, with different causes? Then in your timer you'll have to have a handle to some form of validation that can execute the validation again that threw the original exception in the first place. – CodeCaster Sep 13 '16 at 13:52
  • you would also like to `Stop()` the timer if input is ok in the handler method – slawekwin Sep 13 '16 at 13:58
  • @slawekwin - right. still editing. thanks for the catch –  Sep 13 '16 at 14:04
  • 1
    @CodeCaster - "handle some form of validation" - you are right, which is why I had the "... format is still bad ...". It is up to him to specify some code that verifies the input's validity. "what if one form can generate multiple errors" - then OP should ask about that. Since he doesn't, I give him answer for this one error he is asking about and expect that he will be able to extend the answer to the case where he has multiple things that can generate exceptions. –  Sep 13 '16 at 14:07
  • @Agapwlesu , stupid question but what does OP stand for? and I appreciate the time you took to give me a detailed solution. – JohnChris Sep 13 '16 at 14:12
  • Original Poster, i think. It is how people in SO refer to the person asking the question. –  Sep 13 '16 at 14:17