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?

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, when timer 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?

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

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");

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

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

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();
commented: Thanks :) +3

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.