I think the building of the input string is the culprit here (or possibly some other thing that's not visible in the code sample you've provided).
If you spawn of a task (be it a
Task
, thread pool worker or even a dedicated thread) the time between doing so and it starting to execute should be small.
Take this example program;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1 {
class Program {
static void Log(string message) {
Console.WriteLine("{0:00} @ {1}; {2}", Thread.CurrentThread.ManagedThreadId, DateTime.Now, message);
}
static void SendAsync(object input) {
Log(String.Format("--> SendAsync; {0}", input));
Log("<-- SendAsync");
}
static void Invoke_1(int index) {
ThreadPool.QueueUserWorkItem(SendAsync, index);
}
static void Invoke_2(int index) {
new Task(() => SendAsync(index)).Start();
}
static void Invoke_3(int index) {
Task.Factory.StartNew(() => SendAsync(index));
}
static void Main() {
const int iterations = 100;
for (var i = 0; i < iterations; ++i) {
var index = i;
Log(String.Format("Loop iteration {0}", index));
Invoke_2(index);
}
Log("All dispatched, press any key to terminate...");
Console.ReadKey();
}
}
}
It generates output that looks something like this;
09 @ 26-08-2014 09:28:04; Loop iteration 0
09 @ 26-08-2014 09:28:04; Loop iteration 1
09 @ 26-08-2014 09:28:04; Loop iteration 2
09 @ 26-08-2014 09:28:04; Loop iteration 3
11 @ 26-08-2014 09:28:04; --> SendAsync; 0
11 @ 26-08-2014 09:28:04; <-- SendAsync
10 @ 26-08-2014 09:28:04; --> SendAsync; 1
10 @ 26-08-2014 09:28:04; <-- SendAsync
09 @ 26-08-2014 09:28:04; Loop iteration 4
09 @ 26-08-2014 09:28:04; Loop iteration 5
09 @ 26-08-2014 09:28:04; Loop iteration 6
09 @ 26-08-2014 09:28:04; Loop iteration 7
09 @ 26-08-2014 09:28:04; Loop iteration 8
09 @ 26-08-2014 09:28:04; Loop iteration 9
09 @ 26-08-2014 09:28:04; Loop iteration 10
09 @ 26-08-2014 09:28:04; Loop iteration 11
09 @ 26-08-2014 09:28:04; Loop iteration 12
09 @ 26-08-2014 09:28:04; Loop iteration 13
09 @ 26-08-2014 09:28:04; Loop iteration 14
09 @ 26-08-2014 09:28:04; Loop iteration 15
09 @ 26-08-2014 09:28:04; Loop iteration 16
09 @ 26-08-2014 09:28:04; Loop iteration 17
09 @ 26-08-2014 09:28:04; Loop iteration 18
09 @ 26-08-2014 09:28:04; Loop iteration 19
09 @ 26-08-2014 09:28:04; Loop iteration 20
09 @ 26-08-2014 09:28:04; Loop iteration 21
09 @ 26-08-2014 09:28:04; Loop iteration 22
09 @ 26-08-2014 09:28:04; Loop iteration 23
09 @ 26-08-2014 09:28:04; Loop iteration 24
09 @ 26-08-2014 09:28:04; Loop iteration 25
09 @ 26-08-2014 09:28:04; Loop iteration 26
09 @ 26-08-2014 09:28:04; Loop iteration 27
09 @ 26-08-2014 09:28:04; Loop iteration 28
09 @ 26-08-2014 09:28:04; Loop iteration 29
09 @ 26-08-2014 09:28:04; Loop iteration 30
09 @ 26-08-2014 09:28:04; Loop iteration 31
09 @ 26-08-2014 09:28:04; Loop iteration 32
09 @ 26-08-2014 09:28:04; Loop iteration 33
09 @ 26-08-2014 09:28:04; Loop iteration 34
09 @ 26-08-2014 09:28:04; Loop iteration 35
09 @ 26-08-2014 09:28:04; Loop iteration 36
09 @ 26-08-2014 09:28:04; Loop iteration 37
09 @ 26-08-2014 09:28:04; Loop iteration 38
09 @ 26-08-2014 09:28:04; Loop iteration 39
09 @ 26-08-2014 09:28:04; Loop iteration 40
09 @ 26-08-2014 09:28:04; Loop iteration 41
09 @ 26-08-2014 09:28:04; Loop iteration 42
09 @ 26-08-2014 09:28:04; Loop iteration 43
09 @ 26-08-2014 09:28:04; Loop iteration 44
09 @ 26-08-2014 09:28:04; Loop iteration 45
10 @ 26-08-2014 09:28:04; --> SendAsync; 2
10 @ 26-08-2014 09:28:04; <-- SendAsync
09 @ 26-08-2014 09:28:04; Loop iteration 46
As you can see the delta time between
09 @ 26-08-2014 09:28:04; Loop iteration 0
and
11 @ 26-08-2014 09:28:04; --> SendAsync; 0
is in the sub-second range.
What's slowing your app down is either very heavy lifting involved in creating the input-string or possibly some locking going on, not the way you spawn threads.
Hope this helps,
Fredrik