我希望C#中的计时器在执行后自行销毁.我怎么能实现这个目标?
private void button1_Click(object sender, EventArgs e)
{
ExecuteIn(2000, () =>
{
MessageBox.Show("fsdfs");
});
}
public static void ExecuteIn(int milliseconds, Action action)
{
var timer = new System.Windows.Forms.Timer();
timer.Tick += (s, e) => { action(); };
timer.Interval = milliseconds;
timer.Start();
//timer.Stop();
}
Run Code Online (Sandbox Code Playgroud)
我希望此消息框只显示一次.
我在Windows窗体C#中使用Thread.Sleep.使用哪个导致没有响应表格.使用Timer也不符合我的目的.
示例代码:
if(......) // There are many if with difrent components and many Thread.sleep
{
button.visible=true;
Thread.sleep(1000);
GotoMeasurementMode(3000);
Thread.sleep(3000);
query(device.Text);
Thread.sleep(7000);
StopMeasurement();
Thread.sleep(4000);
}
Run Code Online (Sandbox Code Playgroud)
使用上面的代码会导致形式无响应.使用Timer会产生嵌套计时器.而且在我的情况下并不成功.请告诉我Windows窗体的另一种选择.想要命令之间的特定暂停.