5

I need to control one thread for my own purposes: calculating, waiting, reporting, etc...
In all other cases I'm using the ThreadPool or TaskEx.

In debugger, when I'm doing Thread.Sleep(), I notice that some parts of the UI are becoming less responsible. Though, without debugger seems to work fine.

The question is: If I'm creating new Thread and Sleep()'ing it, can it affect ThreadPool/Tasks?

EDIT: here are code samples:

One random place in my app:

ThreadPool.QueueUserWorkItem((state) =>
{
    LoadImageSource(imageUri, imageSourceRef);
});

Another random place in my app:

var parsedResult = await TaskEx.Run(() => JsonConvert.DeserializeObject<PocoProductItem>(resultString, Constants.JsonSerializerSettings));

My ConcurrentQueue (modified, original is taken from here):

Creation of thread for Queue needs:

public void Process(T request, bool Async = true, bool isRecurssive = false)
{
    if (processThread == null || !processThread.IsAlive)
    {
        processThread = new Thread(ProcessQueue);
        processThread.Name = "Process thread @ " + Environment.TickCount;
        processThread.Start();
    }

If one of the Tasks reports some networking problems, i want this thread to wait a bit

if (ProcessRequest(requestToProcess, true))
{
    RequestQueue.Dequeue();
}
else
{
    DoWhenTaskReturnedFalse();
    Thread.Sleep(3000);
}

So, the question one more time: can Thread.Sleep(3000);, called from new Thread(ProcessQueue);, affect ThreadPool or TaskEx.Run() ?

Vitalii Vasylenko
  • 4,776
  • 5
  • 40
  • 64
  • 2
    It would *really* help if you could show a short but complete program demonstrating the problem. – Jon Skeet May 14 '14 at 14:38
  • Calling Thread.Sleep() on a threadpool thread is very unwise, it just a rock in the road to the threadpool scheduler that cannot see why a tp-thread isn't completing so doesn't know to release another tp-thread. Not otherwise directly related to UI responsiveness, awaitable tasks will bog down, perhaps. You'd always use Task.Delay() instead. – Hans Passant May 14 '14 at 14:44
  • 1
    @JonSkeet Added some samples, hope they would help – Vitalii Vasylenko May 14 '14 at 15:01
  • @HansPassant I'm calling it from newly created `new Thread()`. Can it affects on a ThreadPool? – Vitalii Vasylenko May 14 '14 at 15:02
  • That's not a short but complete program demonstrating the problem, is it? I'd like something I can copy and paste, and use to reproduce the issue. – Jon Skeet May 14 '14 at 15:03
  • This is all pretty unlikely. No idea why you need us to tell you when you can simply not start the thread or freeze it with the debugger. – Hans Passant May 14 '14 at 15:20
  • @HansPassant I'm just wondering, if newly-created task ( `new Task()` ) **is** a still part of a ThreadPool. Is a Sleeping() of my Task can affect the ThreadPool? – Vitalii Vasylenko May 14 '14 at 15:36
  • 2
    If you see odd behavior in the debugger, it's because you're debugging, most often. You shouldn't worry too much about it. Worry about performance at runtime. –  May 14 '14 at 17:04

3 Answers3

4

Assuming that the thread you put on sleep was obtained from thread pool then surely it does affect the thread pool. If you explicitly say that the thread should sleep then it cannot be reused by the thread pool during this time. This may cause the thread pool to spawn new threads if there are some jobs awaiting to be scheduled. Creating a new thread is always expensive - threads are system resources.

You can however look at Task.Delay method (along with async and await) that suspends executing code in a more intelligent way - allowing the thread to be reused during waiting. Refer to this Thread.Sleep vs. Task.Delay article.

nan
  • 19,595
  • 7
  • 48
  • 80
3

Thread.Sleep() affects the thread it's called from, if you're calling Thread.Sleep() in a ThreadPool thread and trying to queue up more it may be hitting the max count of ThreadPool threads and waiting for a thread to finish before executing another.

http://msdn.microsoft.com/en-us/library/system.threading.threadpool.setmaxthreads.aspx

JasonSec
  • 614
  • 5
  • 12
1

No, the Thread.Sleep() is only on the current thread. Thread.Sleep(int32) documentation:

The number of milliseconds for which the thread is suspended.

dcastro
  • 66,540
  • 21
  • 145
  • 155