使用Thread.sleep代码为短的时间问题 [英] Problems with using Thread.Sleep for short times

查看:165
本文介绍了使用Thread.sleep代码为短的时间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个线程(现在的)一个应用程序,但似乎功能Thread.sleep()方法不起作用很好。它睡觉的线程,但它需要更多的时间(例 - 我要睡觉了它5毫秒,它休眠0,3s以上)。这里是code:

I have an app with 2 threads (now), but it seems that function Thread.Sleep() doesn't work very good. It sleeps threads but it takes much more time (for example- I want to sleep it for 5ms and it sleeps for 0,3s or more). Here is code:

int vlakien = 2;
Thread[] vlakna; 
vlakna = new Thread[vlakien];

for (int i = 0; i < vlakien; i++) 
{ try { vlakna[i] = new Thread(new ThreadStart(utok)); vlakna[i].Start(); } }

private void utok()
{
  //some code
  Thread.Sleep(5);
  //some code
}

此外,我试着用秒表在功能utok睡了吧,它也需要更多的时间:

Also I tried to sleep it with Stopwatch in the function utok and it also takes more time:

Stopwatch SW = new Stopwatch(); SW.Start();
while(SW.ElapsedMilliseconds < 5000) ;

请帮忙。

推荐答案

15毫秒是在Windows线程时间片(实际上,你可以惹的窗口,并更改....不是建议)。事情可以给自己的时间片起个大早,但任何可能需要充分的时间片。

15ms is the thread time slice on windows ( you can actually mess with windows and change that.... not at all recommended). Things can give their time slices up early, but anything could take their full time slice.

所以,它真的很难得到任何比这更好的,事实上,现实20或30毫秒的可能性更大。我以前做的其中有了50ms的硬实时限制的实时处理。这行之有效的窗口,如果你遵守一定的规则(这是在C ++中)

So its really hard to get any better than that, in fact, realistically 20 or 30 ms is more likely. I used to do real time processing which had a hard real time limit of 50ms. That worked well on windows if you obeyed certain rules ( it was in C++)

这篇关于使用Thread.sleep代码为短的时间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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