Does dynamically run dynamically on a remote object asynchronously?

I have inherited some basic spaghetti code (C # / VB combination) which I am trying to understand here.

This seems to be a very strange situation where there are two consecutive calls to fire events on a remote object, which is done by calling the DynamicInvoke method of the form delegate:

delegate1.DynamicInvoke(args1);
// some code here
delegate2.DynamicInvoke(args2);

      

delegate1 and delegate2 both refer to methods on the same remote subscriber.

According to everything I can read in the documentation , DynamicInvoke looks like it should be synchronous. But I can see, before my very eyes, when I put breakpoints on the remote process, that the methods referenced by delegate1 and delegate2 run concurrently , on different threads.

Is this another invalid Microsoft feature? Should I expect this? Any explanations why this should be? And if it's designed to run asynchronously, how does DynamicInvoke have a return value?

Thank! Shaul

+1


source to share


2 answers


DynamicInvoke is definitely a synchronous operation. The problem is that the delegate indicates that it is not synchronous. For example, the following code will call Console.WriteLine () in an asynchronous manner.

Action write = () => Console.WriteLine("foo");
Action del1 = () => { ThreadPool.QueueUserWorkItem(write); }
...
del1.DynamicInvoke();

      



del1 will run synchronously, but it will create an asynchronous work item.

I would go looking for the code that the delegate executes and see if they do some ASIC magic under the hood.

+2


source


My main advice would be to carefully check the call stacks in your two processes.



Is it possible that your spaghetti code actually has a callback. Does your first delegate call your remote process, does it call back, and your second delegate gets called from a nested callback?

0


source







All Articles