I have the following code and for some reason the ToArray() method takes 7ms for a single element. The application has several worker threads in which this happens and I have also profiled the application with JetBrains dotTrace which shows that the thread is waiting for another one in this time (and not just waiting for the CPU), which should obviously not be possible because the List is a local variable and I don't use locks.
List<TransactionType> transactionTypes =
new List<TransactionType>(_TransactionTypes.LengthNull() +
drawingAction._TransactionTypes.LengthNull())
...
Stopwatch sw = Stopwatch.StartNew();
_TransactionTypes = transactionTypes.ToArray();
sw.Stop();
if (sw.ElapsedMilliseconds > 3)
{
int deb = 0;
}
This phenomenon occurs pretty much at every point where methods of the .Net framework are used (e.g. String.Format). I had this problem for a while now and for some reason using a SpinWait in the main thread makes it even worse.
Neither I nor my colleagues have an explanation for this and Google didn't help either so I'd by very thankful for help.
Found the problem:
Or at least a part of it. It seems like it was caused by GC pressure and I already know how to reduce it in the application. I have still no clue why SpinWait sometimes caused the blocking of threads but I resorted to just not using it.
This question was marked as a duplicate and Stack Overflow told me to edit it to explain why but to be honest it is pretty damn clear that it isn't if you even bother to read past the title.