111,092
社区成员




public void executeRun()
{
Action showInfoAction = ShowInfo;//Action是一个.net自己的委托,就是一个void、无参形式的委托
this.BeginInvoke(showInfoAction);
}
private void ShowInfo()
{
MessageBox.Show("maopian yijing bofang le" + DateTime.Now.ToString());
}
private void Form1_Load(object sender, EventArgs e)
{
Action action = Run;
action.BeginInvoke(null, null);
}
#region 每隔一段特定的时间执行一次:程序启动后在每隔10秒之后再次执行
public void Run()
{
while (true)
{
executeRun();
Thread.Sleep(1000);
}
}
public void executeRun()
{
this.BeginInvoke(new Action(() => { MessageBox.Show("maopian yijing bofang le" + DateTime.Now.ToString()); }));
}
#endregion
private void Form1_Load(object sender, EventArgs e)
{
Run();
}
#region 每隔一段特定的时间执行一次:程序启动后在每隔10秒之后再次执行
private System.Threading.Timer tt = null;
public void Run()
{
if (tt == null)
{
tt = new System.Threading.Timer(executeRun, null, 5000, 10000);
}
}
public void executeRun(object obj)
{
this.BeginInvoke(new Action(() => { MessageBox.Show("maopian yijing bofang le" + DateTime.Now.ToString()); }));
//timer1.Start();
}
#endregion
使用timer
private System.Threading.Timer _timer = null;//这个要使用个全局变量,代码不好看
private void StartMyWork()
{
_timer = new Timer(MyMethod, null, 0, 100);
}
private void MyMethod(object obj)
{
/*DoSomething*/
//如果在timer里面需要更新UI控件的话,由于这个timer不是在主线程中执行的因此要这样访问:
this.Dispatcher.BeginInvoke(
new Action(
() =>
{
//这样更新UI
}));
}
Thread.Sleep(10*60*1000);
private void form_Load()
{
Action loopAction = MyMethod;
loopAction.BeginInvoke(null,null);
}
private void MyMethod()
{
while(true)
{
//DoSomething
Thread.Sleep(100);
}
}
private void Form1_Load(object sender, EventArgs e)
{
Run();
}
#region 每隔一段特定的时间执行一次:程序启动后在每隔10秒之后再次执行
public void Run()
{
TimerCallback tcb = new TimerCallback(executeRun);
//Timer在5秒中后(DueTime)开始执行,之后每隔10秒(Period)执行一次
System.Threading.Timer tt = new System.Threading.Timer(tcb, null, 5000, 10000);
}
public void executeRun(object obj)
{
//MessageBox.Show("maopian yijing bofang le" + DateTime.Now.ToString());
timer1.Start();
}
#endregion
private void timer1_Tick_1(object sender, EventArgs e)
{
MessageBox.Show("maopian jijiang kaishi");
}
MessageBox.Show("shifoubofangmaopian");
MessageBox.Show("shifoubofangmaopian");