本文介绍了为什么这么Thread.sleep代码有害的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我常常看到它提到, Thread.sleep代码(); 不应该被使用,但我不明白为什么会这样。如果 Thread.sleep代码(); 可引起麻烦,是否有同样的结果,这将是安全的替代解决方案。

如:

 ,而(真)
{
    做一些工作();
    我++;
    Thread.sleep代码(5000);
}

另外一个是:

 ,而(真)
{
    字符串[] =影像Directory.GetFiles(@C:\\目录,*。PNG);    的foreach(在图像串图片)
    {
        this.Invoke(()=> this.Enabled = TRUE);
        pictureBox1.Image =新位图(图片);
        Thread.sleep代码(1000);
    }
}


解决方案

与调用问题 Thread.sleep代码是explained相当简洁这里:

The preferred solution: WaitHandles

The most-made-mistake is using Thread.Sleep with a while-construct (demo and answer, nice blog-entry)

EDIT:
I would like to enhance my answer:

这篇关于为什么这么Thread.sleep代码有害的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:37
查看更多