none
Thread in a threadpool not being executed on time RRS feed

  • Question

  • My Windows Service is written in VB.NET Visual Studio 2010 (.NET framework 4).
    The service is using a threadpool like below:
    ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf clsA.ProcessIt))

    In my case, sometimes the thread is not being executed, even though there are plenty of available threads, also plenty of CPU and memory.
    For example below, there are 32736 available threads in the pool, but yet sub ProcessIt is not being executed until 1 minute later.

    Let's say sub ProcessIt is executed per message.
    It looks like in between the time I call "ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf clsA.ProcessIt))", and sub ProcessIt being executed, the service is doing sub ProcessIt for other messages.

    In the example below, let's say sub ProcessIt is called for message 1 until 10.
    Then, for message 11, even though "ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf clsA.ProcessIt))" is already called, and there are 32736 available threads in the pool, sub ProcessIt for message 11 is not executed until 1 minute later, and in the mean time the service is doing sub ProcessIt for message 1 until 10.

    What can cause this, and how can I fix it ?

                    ThreadPool.GetMaxThreads(x, y)
                     WriteToFile("GetMaxThreads x = " & x & " y = " & y) '--> x = 32767 y = 1000
                     ThreadPool.GetAvailableThreads(x, y)
                     WriteToFile("GetAvailableThreads x = " & x & " y = " & y) '--> x = 32736 y = 1000
                     ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf clsA.ProcessIt))

    Thank you.

    • Moved by Carl Cai Tuesday, November 11, 2014 1:35 AM better place
    Monday, November 10, 2014 9:13 PM

Answers

  • Hello aujong,

    >>What can cause this, and how can I fix it ?

    From your description, it seems that you would create a number of tasks in a short period of time, if it is, according to the document from MSND:

    As part of its thread management strategy, the thread pool delays before creating threads. Therefore, when a number of tasks are queued in a short period of time, there can be a significant delay before all the tasks are started.

    So the ThreadPool will delay creating new threads, adding several items at once is almost guaranteed to incur a delay. Please check if you are under this scenario.

    If not, for narrowing this issue, my suggestion is you could write the callback method as simple as possible and ensure there would not a resource competition, for example:

    ThreadPool.QueueUserWorkItem(o => Console.WriteLine("Thread " + Thread.CurrentThread.ManagedThreadId + " is executing"));

    This would help narrow down whether this issue is related the callback method or the threadpool.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 11, 2014 6:40 AM
    Moderator

All replies

  • Hello,

    I have helped you move this thread to the forum which is more dedicated for this issue.

    Regards.

    Carl


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 11, 2014 1:37 AM
  • Hello aujong,

    >>What can cause this, and how can I fix it ?

    From your description, it seems that you would create a number of tasks in a short period of time, if it is, according to the document from MSND:

    As part of its thread management strategy, the thread pool delays before creating threads. Therefore, when a number of tasks are queued in a short period of time, there can be a significant delay before all the tasks are started.

    So the ThreadPool will delay creating new threads, adding several items at once is almost guaranteed to incur a delay. Please check if you are under this scenario.

    If not, for narrowing this issue, my suggestion is you could write the callback method as simple as possible and ensure there would not a resource competition, for example:

    ThreadPool.QueueUserWorkItem(o => Console.WriteLine("Thread " + Thread.CurrentThread.ManagedThreadId + " is executing"));

    This would help narrow down whether this issue is related the callback method or the threadpool.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 11, 2014 6:40 AM
    Moderator
  • Thank you for your reply.

    Yes, in my case, we are calling the "ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf clsA.ProcessIt))" for many messages around the same time.

    So, I can't do that ?

    Tuesday, November 11, 2014 2:57 PM
  • Hello aujong,

    >>So, I can't do that ?

    If you mean that you cannot ensure the thread in a threadpool being executed on time, according to the MSDN document I provide, I am afraid it is.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 20, 2014 7:26 AM
    Moderator