This question has already been answered:
If System.Threading.Thread.Sleep (1000) is used in a loop, the entire program is paused. I need to, during a pause in the loop, I can click on the Windows Forms buttons.
This question has already been answered:
If System.Threading.Thread.Sleep (1000) is used in a loop, the entire program is paused. I need to, during a pause in the loop, I can click on the Windows Forms buttons.
A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .
A simple example of starting a timer and then using it. The first button starts the timer, which increases i. The second button is to place i in the field. This way you can solve your problem. The program will not hang.
public partial class Form1 : Form { public Form1() { InitializeComponent(); } int i = 0; private void button1_Click(object sender, EventArgs e) { timer1.Interval = 1000; timer1.Enabled = true; } private void button2_Click(object sender, EventArgs e) { textBox1.Text = i.ToString(); } private void timer1_Tick(object sender, EventArgs e) { i++; } } }
UPD In your case, there will not be a cycle, but the necessary code will be called in the timer itself (for example, there will be a call to a function that you previously had in the cycle)
To stop the loop, you can use timer1.Enabled = false; which is called when a button is pressed
Source: https://ru.stackoverflow.com/questions/785114/
All Articles