Code Comments
Programming Forum and web based access to our favorite programming groups.Xref: newsfeed-west.nntpserver.com microsoft.public.dotnet.csharp.general:46 60 microsoft.public.dotnet.languages.csharp:414909 I am creating a pub/sub broker implementation in C# and I am having some trouble understanding how I should best implement the invoke portion in order to make sure it is as thread-safe as it can be. The "broker" class allows other objects to subscribe to messages that will get published by other objects. To subscribe they pass in a Object that represents thier instance (aka 'this') and a String that contains the method name to be invoked. When a particular message is published the instance and method name are used to forward the message (and any data that was sent with it) to the subscriber so they can process it. Each message may have zero or more subscribers as well. To process the queued messages I am using a System.Timers.Timer class which uses a separate thread to handle the delay between the timer events. Inside the Timer elapsed event I am forwarding all of the queued messages to any subscribers that are interrested in them. To perform the Invoke I am doing the following which I believe will cause problems. Instance.GetType().GetMethod(MethodName).Invoke(Instance, Parms); Where Instance is an Object that the subscriber passed in (aka 'this'), MethodName is a String that contains a public method to be invoked, and Parms is a Object[] that contains the parameters that the method is expecting. How thread-safe is this since there are at least 2 threads being used (whatever the application thread is and the Timer thread.) I will not know ahead of time what the subscriber actually is - plain class, WinForms class, etc. I have seen mention of ISynchronizeInvoke, Invoke, BeginInvoke and EndInvoke but I am not sure exactly how they play into this sort of environment. Any suggestions or input would be appreciated. Chuck
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.