替代Thead.Sleep?在C# [英] Alternative to Thead.Sleep? in C#

查看:124
本文介绍了替代Thead.Sleep?在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有另一个线程轮询用户输入。此线是主线程,并且可以在一个时间入睡分钟。如果用户点击想退出也可能是2+分钟前控制台窗口关闭这可能会觉得没有反应。

I have another thread polling user input. This thread is the main thread and can sleep minutes at a time. If the user hits wants to quit it may be 2+ minutes before the console window shuts down which may feel not responsive.

我怎样才能使这个线程的唤醒了Thread.Sleep?我是否需要使用另一个函数睡觉,传递一个布尔值裁判它来检查当线程醒来并结束?

How can i make this thread wake up from Thread.Sleep? Do i need to use another function to sleep and pass in a bool ref for it to check when the thread wake up and end?

推荐答案

使用 Monitor.Wait代替,并呼吁Monitor.Pulse或Monitor.PulseAll 唤醒线程了。

这篇关于替代Thead.Sleep?在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆