views:

45

answers:

2

Example:

I add a PostSharp attribute to a method to ensure a stopwatch is started before the method is called, and stopped just after the call returns. This method is used in a web app, so it will be called by multiple threads.

I store the result of the stopwatch timing in a static threadsafe collection, for use by all threads. This collection can then be read by another monitoring thread for the purposes of analysis.

Will this effectively force all method calls to block while they wait for the lock on the collection to be released? (so that they can complete the postsharp code block).

Would asynchronous messaging using MSMQ enable a non-blocking solution to this problem?

PostSharp Attribute code:

//...

public static ThreadSafeCollection _collection;

public override void  OnInvocation(MethodInvocationEventArgs eventArgs)
{
  var start = DateTime.Now;
  eventArgs.Proceed();
  var timeSpent = (DateTime.Now - start).TotalMilliseconds;

  _collection.Add(timeSpent); //will this cause all 
                              //method calls to methods 
                              //decorated with this 
                              //attribute to block on the 
                              //_collection addition?
}

//...

+1  A: 

Will this effectively force all method calls to block while they wait for the lock on the collection to be released? (so that they can complete the postsharp code block).

Yes

Would asynchronous messaging using MSMQ enable a non-blocking solution to this problem?

Maybe. The internals details of how msmq handles send request are not documented ( as far as I know).

Other option would be to use a thread specific (not synchronized) collection. And move its content to a shared collection ( not synchronized ) only when the lock on the shared collection is free.

Igal Serban
+1  A: 

You should rather use OnMethodBoundaryAspect because OnMethodInvocationAspect has significant overhead. Given that the effect of your aspect is very lightweight, PostSharp 2.0 Professional Edition would generate much more efficient code.

Gael Fraiteur