private int data1 = 0;
int Step1 = 0; //标志位
int status2 = 0; //标志位
private async void timer1_Tick(object sender, EventArgs e)
{
if (status2 == 1) return; // 忙就不进
status2 = 1; // ✅ 立刻上锁(关键)
try { switch (Step1) { case 0: textBoxStart.Text = "开始"; Step1 = 10; break; case 10: textBoxStart.Text = "步序是10"; Step1 = 20; break; case 20: textBoxStart.Text = "步骤20"; Step1 = 30; // ✅ 先推进步序 await Task.Delay(3000); // ✅ 延时才稳定 break; case 30: textBoxStart.Text = "步骤30"; Step1 = 40; break; case 40: textBoxStart.Text = "步骤40"; timer1.Enabled = false; // ✅ 结束就停(否则会一直刷40) break; } } catch (Exception ex) { timer1.Enabled = false; textBoxStart.Text = "异常:" + ex.Message; Step1 = 0; } finally { status2 = 0; // ✅ 解锁 }}
//------------备注-------------------
1️⃣ Step 一定要在 await 前推进
2️⃣ 锁只保证“单次 Tick 不重入”,不是保证整个 Delay 期间