Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

Initializing a dynamic variable inside an asyncrounous task

$
0
0

I want to perform some methods on a dynamic variable, say (C# and this is just a proof of concept):

:
object returnValue;
dynamic myObject;
Type someDynamicClass = Type.GetTypeFromProgID("SomeCOMClass");
myObject = Activator.CreateInstance(someDynamicClass);
returnValue = myObject.ReturnSomething();  // Just to "precompile" the dynamic outside the loop
for (int ix = 0; ix < 1000; ix++)
{
   returnValue = myObject.ReturnSomething();
}
:

Now this works pretty fine, running on the main thread (WPF application), however the first call (outside the loop) is slow due to dynamic's reflection work, method caching etc. (if I do the first call inside the loop, the loop execution taks approx. 1350 ms longer in my case).

For practical reasons I want to do this "precompilation" asynchroneously at application startup and therefore moved the myObject instantiation and the first call into a Task (which is then performed on a worker thread, not the main thread as before. I do necessary synch so not executing the loop simultaneously with the task.
But now the loop takes approx. 2 times longer time to execute and I do not understand why.
If I move the myObject instantiation back into the main thread (just leaving the first call in the Task), then the speed is optimal again and this solves my problem, I just don't understand why.

Someone care to explain ?


Viewing all articles
Browse latest Browse all 1710

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>