温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

c# 之下如何做定时

发布时间:2020-06-28 10:26:04 来源:网络 阅读:566 作者:古泥瓦 栏目:编程语言

先如下定义一个定时器:

public DispatcherTimer dispatcherTimer;

然后在某处创建这个对象实例:

dispatcherTimer = new System.Windows.Threading.DispatcherTimer();

设定超时回调函数:

dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

设定间隔(下方例子是10秒钟):

dispatcherTimer.Interval = new TimeSpan(0, 0, 10);

启动定时器:

dispatcherTimer.Start();

定义超时回调函数:

private void dispatcherTimer_Tick(object sender, EventArgs e)

{

// do something here...

}


值得注意的是DispatcherTimer是无法直接在console下运行的,需要额外的东西。

下面文字来自http://stackoverflow.com/questions/19351473/dispatchertimer-doesnt-work-in-console的解释

The console and unit test environment by default don't have a dispatcher to run your dispatcher timer.

You can still use Dispatcher.CurrentDispatcher to create a Dispatcher to run your code.



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI