什么是Thread.sleep代码(1)在C#的影响? [英] What is the impact of Thread.Sleep(1) in C#?

查看:1038
本文介绍了什么是Thread.sleep代码(1)在C#的影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows窗体应用程序是什么调用的影响的Thread.Sleep(1)所示在以下code:

In a windows form application what is the impact of calling Thread.Sleep(1) as illustrated in the following code:

public Constructor()
{
    Thread thread = new Thread(Task);
    thread.IsBackground = true;
    thread.Start();
}

private void Task()
{
    while (true)
    {
        // do something
        Thread.Sleep(1);
    }
}

请问这个线程猪所有可用的CPU?

Will this thread hog all of the available CPU?

我可以使用哪些分析方法来衡量此线索的CPU使用率(除任务管理器等)?

What profiling techniques can I use to measure this Thread's CPU usage ( other than task manager )?

推荐答案

如前所述,你的循环不会养猪的C​​PU。

As stated, your loop will not hog the CPU.

但要注意的:Windows是的不可以一个实时操作系统,所以你的不可以获得1000每秒醒来从Thread.sleep代码( 1)。如果你还没有使用 timeBeginPeriod 设置您的最小分辨率当你醒来大约每15毫秒。你设置的最小分辨率为1毫秒即使,你仍然只醒来每3-4毫秒。

But beware: Windows is not a real-time OS, so you will not get 1000 wakes per second from Thread.Sleep(1). If you haven't used timeBeginPeriod to set your minimum resolution you'll wake about every 15 ms. Even after you've set the minimum resolution to 1 ms, you'll still only wake up every 3-4 ms.

为了获得毫秒级定时器粒度您必须使用Win32多媒体定时器( C#包装)。

In order to get millisecond level timer granularity you have to use the Win32 multimedia timer (C# wrapper).

这篇关于什么是Thread.sleep代码(1)在C#的影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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