c# winfrom 在while循环里的延时
答案:6 悬赏:40
解决时间 2021-01-17 02:52
- 提问者网友:活着好累
- 2021-01-16 22:11
c# winfrom 在while循环里的延时
最佳答案
- 二级知识专家网友:青尢
- 2021-01-16 22:38
private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(Sleep);
thread.Start();
thread.Join();
MessageBox.Show("阻塞成功!");
}
public void Sleep()
{
Thread.Sleep(2000);
}
如果出现假死你可以试试backgroundworker
{
Thread thread = new Thread(Sleep);
thread.Start();
thread.Join();
MessageBox.Show("阻塞成功!");
}
public void Sleep()
{
Thread.Sleep(2000);
}
如果出现假死你可以试试backgroundworker
全部回答
- 1楼网友:鸠书
- 2021-01-17 04:43
2000 改成200 可能好一点追问但是我就想要2秒啊!!追答那个你加个提示信息吧。。。2秒钟肯定会卡一会。。。可以考虑线程实现。。
- 2楼网友:人间朝暮
- 2021-01-17 03:24
主线程就是你程序界面线程 在这个线程里睡眠界面必然无法反映 新建一个线程睡眠就好了
- 3楼网友:孤独入客枕
- 2021-01-17 02:30
System.Threading.Timer timerlisten; 你可以用这个时间类,多线程的
timerlisten = new System.Threading.Timer(new TimerCallback(displayProgess), this, 5000, 100);
void displayProgess(object obj)
{
你想做的事
}
也就会避免你的界面假死的情况了
不好意思,如果你不要用timer控件的话
thread = new System.Threading.Thread(
new System.Threading.ThreadStart(monitor));
thread.Start();
新建一个线程也就OK了
timerlisten = new System.Threading.Timer(new TimerCallback(displayProgess), this, 5000, 100);
void displayProgess(object obj)
{
你想做的事
}
也就会避免你的界面假死的情况了
不好意思,如果你不要用timer控件的话
thread = new System.Threading.Thread(
new System.Threading.ThreadStart(monitor));
thread.Start();
新建一个线程也就OK了
- 4楼网友:纵马山川剑自提
- 2021-01-17 01:06
通常耗时的工作都会从主线程时剥离出来,让他在线程中执行,以避免UI假死
- 5楼网友:酒安江南
- 2021-01-17 00:06
///
/// 自定义延时函数
///
/// 延时毫秒
public void Delay(int ms)
{
int i = ms / 10;
while (i-->0)
{
Application.DoEvents();
System.Threading.Thread.Sleep(10);
}
}
/// 自定义延时函数
///
/// 延时毫秒
public void Delay(int ms)
{
int i = ms / 10;
while (i-->0)
{
Application.DoEvents();
System.Threading.Thread.Sleep(10);
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息