Click here to Skip to main content
15,918,516 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

C#
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
            }
        }
Posted
Updated 6-Sep-15 17:27pm
v4

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900