Oscar,
Unfortunately, calling DynamicInvoke will not cause the delegate to be
invoked asynchronously. It only causes the delegate to be invoked on the
calling thread.
You need to call the BeginInvoke method on the delegate to be called.
Also, you should wrap this up in a utility method.
There is one problem. If you make a call to BeginInvoke, you are going
to end up taking threads from the thread pool to process your writes to the
log. Your pages are processed on this same thread pool. If you have a good
number of writes, you are going to impact the performance of your app.
Finally, make sure that you create a delegate to be called when the
asynchronous call is completed. Calling a delegate asynchronously leaks a
ManualResetEvent which you will need to close down manually. In the event
handler, call Dispose on the WaitHandle exposed by AsyncWaitHandle on the
IAsyncResult passed into the method. You could let the GC handle them (and
they will be cleaned up by a GC eventually), and it might even be viable in
an ASP.NET application, since GC's occur on a more predictable schedule
(assuming traffic patterns are steady).
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"Oscar Thornell" <oscar.thornell [ xx] gmail.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
Hi,
I am thinking about doing all my logging asynchronously using a delegate.
The main resaon for this would be performance and responsiveness of the
application (an ASP.NET app).
//Exampel
delegate void LogDelegate(string message);
LogDelegate logger = new LogDelegate(Log.Debug);
logger.DynamicInvoke("Some message..");
I appreciate feedback about the approach/idea! Like the overhead of
creating a delegate and so on...
Regards
/Oscar