7 Replies - 607 Views - Last Post: 23 March 2012 - 04:13 PM Rate Topic: -----

#1 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Timer won't obey

Posted 22 March 2012 - 10:29 AM

So this is a timer related question, I don't use it a lot and am not very experienced with programming so this might be something I'm doing wrong somewhere else in the code, but I'm not sure, and so I thought I'd only post this part of it to keep it brief for you all.

So the thing is, I made a timer1_Tick, and within that, I.. I'll show you a less cluttered example, and then the actual code.

The less cluttered example:

void timer1_Tick(object sender, EventArgs e)
{
    if (textBox1.Text.Contains("specific text"))
    {
      textBox1.Text = "Try again"; 
      timer1.Enabled = false; //I was under the impression that this would stop it from running again
    }

    else 
    {
      textBox1.Text = "Another text"; 
      timer1.Enabled = false;
    }
}



So there, I had hoped that if it goes into the if, and runs that, that timer1.Enabled = false; would stop it from running after it runs the if part and only the if part. And it does, for the first time. However the second time I enter an invalid value, it runs the main if, and then it goes back into the void and runs the else part. And I have no idea what causes it.

A brief clarification of what I'm doing in the real code that causes the error:

I enter an account number, and a pin number. Both of those I leave blank, so both of those values will become null. It (the client) then sends the values to the server, and the server sends back the reply: "Either the account number or pin number was invalid".

And when this happens, I start running the timer1_Tick, and seeing as the if checks the textBox for the word invalid, what I wanted it to do, was to change the text in the textBox (among other things) and then the timer1.Enabled = false was supposed to stop running the timer, and definitely not run the else part.

How it actually works:

The first time I enter a null and null for both the acc and the pin, it does exactly that.

However, the second time I do that (in a row) it runs the if part and then carries on to run the else part and I have no idea why.

So erm, can anyone tell what I'm doing wrong from the code below?

            void timer1_Tick(object sender, EventArgs e)
        {
            if (textBox1.Text.Contains("invalid")) //If the user received an "invalid login" message from the server, it should appear in the textbox and so the part below runs
            {
                accountNumer = null; //Reset the accountNumber variable
                pinNumer = null; //Reset the pinNumber variable
                textBox1.Text = "Welcome!\r\nPlease enter your account number: " + accountNumer + "\r\nEnter your pin number: " + pinNumer; //Displays a welcome message in the textBox and asks for the user's account and pin number again
                teljari = 0;  
                timer1.Enabled = false; //This should shut down the timer, or so I thought
            }

            else //If the user does NOT get the invalid login message, this will display the main menu
            {
                textBox1.Text = "Main Menu\r\n 1 - View my balance\r\n 2 - Withdraw Cash\r\n 3 - Deposit funds\r\n 4 - Exit";
                timer1.Enabled = false;
            }
            
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Timer won't obey

#2 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 69
  • View blog
  • Posts: 314
  • Joined: 12-January 11

Re: Timer won't obey

Posted 22 March 2012 - 10:40 AM

Have you tried using
timer1.Stop;



Instead of .Enabled?
Was This Post Helpful? 2
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,645
  • Joined: 02-June 10

Re: Timer won't obey

Posted 22 March 2012 - 10:56 AM

Psyguy: They do the same thing. The advantage of .Enabled = false is that you can make something dynamically respond a little cleaner and in one line without a big if...else construct.
timer1.Enabled = textbox1.Text.ToLower().Contains("invalid");

for example.


Tenderfoot: You might want to cast your comparrison string .ToLower like I've done in the example, just to be safe. "Invalid" != "invalid"

A couple habits to fix: Rename your controls as soon as you make them.
txtFeedback is a lot easier to read and follow code intention than textbox1

You should put a breakpoint at line 3 of your second code block and walk through execution with the F10 key looking at your Locals pallet. I suspect you are throwing an exception with

05 accountNumer = null; //Reset the accountNumber
06 pinNumer = null; //Reset the pinNumber variable
variable


Its possible accountNumer is not a nullable type, and thus throwing an exception so the rest of your code never exectutes




Tenderfoot: What this shows us is that you aren't familiar with breakpoints and how to debug your own code. Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute. Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Mar 2012
Spoiler





Some of my common tips (some may apply more than others to your specific style):
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.


  • I strongly suggest installing VMware or some other virtualization technology on your development PC so you can create a couple virtual computers for testing. This would allow you to debug and test inside: WinXP32, XP64, Vista, Win7x32, Win7x64... etc. without having to actually have 5 physical PC's. Visual Studio will let you send the debug directly into one of these virtual machines so you can watch it operate, check its variables, see the crashes and so on just as if it were debugging on your real machine.

  • This can't be stressed enough in today's world of cell phone messaging:
    Don't use txt/sms/leet/T9 speak like: u no, u r, dnt, wut i m do-n, coz, al gud, b4, ny1, and so on like this guy:

    Spoiler





Was This Post Helpful? 2
  • +
  • -

#4 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Re: Timer won't obey

Posted 22 March 2012 - 03:10 PM

View PosttlhIn`toq, on 22 March 2012 - 10:56 AM, said:

Tenderfoot: You might want to cast your comparrison string .ToLower like I've done in the example, just to be safe. "Invalid" != "invalid"


The reason for why I did that is because I know it'll always be lowercase anyway, seeing as it's ingrained in the code and I wrote it that way.

View PosttlhIn`toq, on 22 March 2012 - 10:56 AM, said:

A couple habits to fix: Rename your controls as soon as you make them.
txtFeedback is a lot easier to read and follow code intention than textbox1


Definitely agreed and I do do that in general, the reason for textBox1 there is that it's the only textbox on the form and I didn't see it being a problem for me.

View PosttlhIn`toq, on 22 March 2012 - 10:56 AM, said:

Tenderfoot: What this shows us is that you aren't familiar with breakpoints and how to debug your own code. Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.


Aye, it wasn't actually a part of my course, at least not in depth, but I was aware of breakpoints. However the extent of my knowledge was that I could place one there and then F11 through it, I honestly don't even know what the difference between F11 and F10 there is at all. And I will definitely have a look at the guides you posted.

As for now, I think I'll rest for the night and then I'll take a closer look at it.

Thank you for your help tlhIn`toq and Psyguy, I'll vote up your posts for this. ^^
Was This Post Helpful? 0
  • +
  • -

#5 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Re: Timer won't obey

Posted 22 March 2012 - 03:28 PM

I still have no idea why this happens, I'll try to explain it better, step by step.

First time I run it, it runs fine:

        void timer1_Tick(object sender, EventArgs e)
        { //Runs first 
            if (textBox1.Text.Contains("invalid")) //Runs second
            {
                accountNumer = null; //Everything in here runs
                pinNumer = null;
                textBox1.Text = "Welcome!\r\nPlease enter your account number: " + accountNumer + "\r\nEnter your pin number: " + pinNumer;
                teljari = 0;
                timer1.Enabled = false; //Ends it
            }

            else //Does NOT run
            {
                textBox1.Text = "Main Menu\r\n 1 - View my balance\r\n 2 - Withdraw Cash\r\n 3 - Deposit funds\r\n 4 - Exit";
                timer1.Enabled = false;
            }
            
        }



So it works, just the way I wanted it to.

Now.. the second time.. this happens..

        void timer1_Tick(object sender, EventArgs e)
        { //Runs first 
            if (textBox1.Text.Contains("invalid")) //Runs second
            {
                accountNumer = null; //Everything in here runs
                pinNumer = null;
                textBox1.Text = "Welcome!\r\nPlease enter your account number: " + accountNumer + "\r\nEnter your pin number: " + pinNumer;
                teljari = 0;
                timer1.Enabled = false;
            }

            //Then it stops here, and goes back to void immediately 

            else //THEN it runs this, which it's not supposed to, it's not even supposed to run the void again
            {
                textBox1.Text = "Main Menu\r\n 1 - View my balance\r\n 2 - Withdraw Cash\r\n 3 - Deposit funds\r\n 4 - Exit";
                timer1.Enabled = false;
            }
            
        }



So that's it. The second time it runs what it's supposed to run, the if, then it goes back to the void after that, and goes to the else and runs that.

So why, why on earth does it go back to void and run the else part?

I mean, I get why it runs the else part, since it goes back to the void in the first place and the text in textbox has changed.

But yeah, does this make any sense to you?
Was This Post Helpful? 0
  • +
  • -

#6 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Re: Timer won't obey

Posted 22 March 2012 - 04:02 PM

On a side note, I can get around this by adding:

if (textBox1.Text.Contains("invalid") || textBox1.Text.Contains("Welcome"))


But if/by doing that I just feel like an idiot dancing around some bug that I don't understand by blocking it and by doing so, running the same thing twice in a row. So.. yeah..

I need some rest for now, but I'd really appreciate it if someone has some explanation for this.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,645
  • Joined: 02-June 10

Re: Timer won't obey

Posted 23 March 2012 - 01:10 PM

Quote

The second time it runs what it's supposed to run, the if, then it goes back to the void after that, and goes to the else and runs that.


Understand: The timer does not stop while you are responding to a .Tick event.

A couple things could be happening.
First, the timer is still running while you are responding the the first .Tick event. If the .Interval is real small then a second call is being made to the handler before the first one is complete. Maybe move Timer.Enabled to the top of the method handler so it gets executed BEFORE you start making changes to the textbox.

Second, if you have subscribed to the .Tick event twice, then it will call the handler twice. Subscribe 3 times and it will respond 3 times for ever 1 .Tick
Was This Post Helpful? 1
  • +
  • -

#8 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Re: Timer won't obey

Posted 23 March 2012 - 04:13 PM

View PosttlhIn`toq, on 23 March 2012 - 01:10 PM, said:

Quote

The second time it runs what it's supposed to run, the if, then it goes back to the void after that, and goes to the else and runs that.

Second, if you have subscribed to the .Tick event twice, then it will call the handler twice. Subscribe 3 times and it will respond 3 times for ever 1 .Tick


That's almost definitely it. I'll look into it in a moment but I'm almost sure. Already up voted your post, thanks :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1