954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

While loop not working :S

Hey there

so i made this code, and i do not get why my while loop doesn't work, it checks once and then it continues to the next line, this is my code:

private void Timer1_Tick(object sender, EventArgs e)
        {
            timer++;
            if (timer != 65)
            {
                timerdone = false;
            }
            else
            {
                timerdone = true;
                Timer1.Stop();
            }
        }

        private void Report_DoWork(object sender, DoWorkEventArgs e)
        {
            while (timerdone ==  true)
            {
                MessageBox.Show("TEST");
            }
        }


any idea of whats wrong :S?

Jazerix
Junior Poster in Training
84 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

What I notice here is that you haven't started the timer (Timer1.Start()) which will trigger the Timer1_Tick event.
Double click the form, a new event will be generated called Form1_Load, this event is triggered when the form is first loaded which in turn will start the timer.

private void Form1_Load(object sender, EventArgs e)
        {
            Timer1.Start();
        }

What is "Report_DoWork" ? A click event for a Button?
Assuming you start the timer, and assuming Report_DoWork is a button, whentimer reaches 65, timerdone will be true, so when you click the button the while loop will be executed which will create an infinite loop, is this what you want?
What exactly are you trying to achieve with this code?

codes4f0x0d
Newbie Poster
7 posts since Jul 2011
Reputation Points: 13
Solved Threads: 5
 

Well first of all, my code does start the timer :P

secondly, "Report_DoWork", is a backgroundworker that i use for this

I want my application to show the message test once it gets to 65

Jazerix
Junior Poster in Training
84 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

while loops only loop as long as the expression evaluates to true. So once it checks the value, and its false it continues on. This would work if you wanted it to continuously show "TEST".

while(true)
    if(timerdone)
        MessageBox.Show("TEST");

Or if you only wanted it to show once:

while(!timerdone) ;
MessageBox.Show("TEST");
nmaillet
Posting Whiz in Training
236 posts since Aug 2008
Reputation Points: 69
Solved Threads: 53
 

See thats what i dont get, ive been working with "while" before and it have always worked :S

As far as i know, its supposed to loop it becomes valid

Jazerix
Junior Poster in Training
84 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

You know what, i think im getting it now, ill report back in a moment and tell you if it worked :)

Jazerix
Junior Poster in Training
84 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Just to clarify:

while (A)
{
    some_method();
}
other_method();

is executed like:

If A is false go to line 4.
    some_method();
    Go to line 1.
other_method();
nmaillet
Posting Whiz in Training
236 posts since Aug 2008
Reputation Points: 69
Solved Threads: 53
 

Got it working now :D

private void Report_DoWork(object sender, DoWorkEventArgs e)
{
while (timerdone != true)
{
Status.Text = "Waiting for timer! " + timer.ToString() + "/65 Left";
}
MessageBox.Show("DONE");
}

Looks awesome! Love you guys thanks for the help :)

Jazerix
Junior Poster in Training
84 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You