关于延迟时间的问题 [英] a question about delay time

查看:111
本文介绍了关于延迟时间的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建一个线程来做一些工作时,线程包含'while'循环,如果循环不使用thread.sleep方法来延迟,窗口将会很慢。

所以问题是我决定使用多少延迟时间。

例如:

如果我使用10ms延迟,窗口很慢,如果我使用100ms延迟,也许窗口不会慢。

不同的电脑有不同的性能,在不同的电脑操作上延迟100毫秒并不总是不慢

所以,应该延迟多少时间我决定了?



When i create a thread to do some work,the thread contains 'while' loop,if the loop don't use thread.sleep method to delay,the window will be very slow.
so the question is how many delay time i decided to use.
for example:
if i use 10ms to delay,the window slow,if i use 100ms to delay,maybe the window will not slow.
different computers have different performance, delay of 100 milliseconds on a different computer operation is not always not slow
so,how many delay time should i decided?

thNcFill = new Thread(new ParameterizedThreadStart(thNcCode));
thNcFill.Start();

        void thNcCode(object oState)
        {
            m_busy = true;
            m_cancelThread = false;
            int iququeNcCount = queueNcCode.Count;

            while (!m_cancelThread)
            {

                if ((iRowIndex == iRowCount) && (iRowIndex > 0))
                {
                    iRowIndex = 0;
                    iRowCount = 0;
                    break;
                }

                try
                {
                    if (iququeNcCount > 0)
                    {
                        string ncCodeContext = queueNcCode.Dequeue();


                        if (ncCodeContext != null)
                        {

                            ncResolver.GetCurrentRow(ncCodeContext, iRowIndex, iRowCount - 1);
                            iRowIndex++;
                        }

                        iququeNcCount--;
                    }

                }
                catch (Exception ncCodeException)
                {
                    if ((ncCodeException as ThreadAbortException) == null)
                    {
                        logTracer.SendTracerMessage(new TracerMessage(TraceType.ERROR, "processing error(rowNumber:" + (iRowIndex + 1).ToString() + "):" + ncCodeException.Message));

                        iRowIndex = 0;
                        iRowCount = 0;
                        Console.WriteLine(DateTime.Now.ToString());
                    }
                    break;
                }
                m_busy = false;
                m_resetEvent.Set();

                System.Threading.Thread.Sleep(10);  //this is the question about delay time
            }
        }

推荐答案

您指定的延迟是所谓的挂钟时间,而不是CPU周期。因此,如果您为延迟指定10 ms,则应该关闭到不同计算机上的那个:

The delay you specify is so called wall clock time, not CPU cycles. So if you specify 10 ms for the delay, it should be close to that on different computers:

系统时钟以称为时钟分辨率的特定速率滴答。实际超时可能不完全是指定的超时,因为指定的超时将调整为与时钟周期一致。

The system clock ticks at a specific rate called the clock resolution. The actual timeout might not be exactly the specified timeout, because the specified timeout will be adjusted to coincide with clock ticks.



但是,如果您需要让前台应用程序更具响应性,请考虑指定优先级 [ ^ ]线程而不是等待特定的时间。通过这种方式,线程可以尽快完成它的工作,而不会过多地影响应用程序。


However, if you need to let the foreground application to be more responsive, consider specifying the Priority[^] of the thread instead of waiting for specific amount of time. This way the thread could complete it's work as soon as possible without too affecting the application too much.


这篇关于关于延迟时间的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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