: hi i have gui with two buttons
: button1 button2
: when i press button1 the programs starts loop
: for (;)
: i want to press button2 and then that var will be changed the problem is during the run the button 2 seems locked i want to press several time button2 and see thing changing
: should i define somthing about button2 or for the gui
As long as a loop is looping, the program will not respond, because the eventmessage loop(the code which handles MouseClick etc.; automatic generated) can't continue looping. You have to give your program time to react on events.
I always use a Timer based loop:
- declare the variables used in the "for"-loop as privates of the Form.
- put the "dosomthing"-code in the Timer_Tick
If the "dosomthing"-code needs no much time and you have to run the loop fast, you can also put the "dosomthing"-code between a "timestoper"-loop:
object var = GIVEmeTHEstartVAR(); // your var classwide available
Timer.Interval = 1; // choose like you want
// start and stop the loop
// change var even while loop is looping
var = GIVEmeAotherVAR(var);
// "timestoper"-loop, ends than "dosomething"-code ran enough for this step( 50 ms )
DateTime DT = DateTime.Now;
while((DateTime.Now - DT).TotalMilliseconds < 50)
// the loop step ends and the program can respond to other events, like Button1_Click, Button2_Click or Form_Close
// After Timer.Intervall-Milliseconds the Timer_Tick is going to run again
Maybe there is a more elegant or difficult(multithreading) method, but this works and is fast.