/// <summary> /// Calls a singlecast event, returning a value /// </summary> static public object callsync(IEventObject obj, string name, bool bSync, params object[] args) { //Play safe? if (bSync) { using (DdMonitor.Lock(obj._sync)) { //Does the event exist? HandlerList list; if (!obj.events.TryGetValue(name, out list)) { //No? No need to worry return(null); } //Got it! Execute all handlers foreach (CEventHandler eh in list.methods) { //Sanity checks if (eh.argnum != args.Length) { throw new ArgumentException("Parameter mismatch while attempting to invoke custom event '" + name + "'"); } //We're ok, time to invoke it object ret = null; //Use the logger if necessary using (LogAssume.Assume(obj._eventLogger)) { //Custom handler? if (eh.customcaller == null) { ret = eh.handler(eh.that, args); } else { //Allow them to call it ret = eh.customcaller(eh, true, eh.that, args); } //Got our loot, return with it return(ret); } } return(null); } } else { //Does the event exist? HandlerList list; if (!obj.events.TryGetValue(name, out list)) { //No? No need to worry return(null); } //Got it! Execute all handlers foreach (CEventHandler eh in list.methods) { //Sanity checks if (eh.argnum != args.Length) { throw new ArgumentException("Parameter mismatch while attempting to invoke custom event '" + name + "'"); } //We're ok, time to invoke it object ret = null; //Use the logger if necessary using (LogAssume.Assume(obj._eventLogger)) { //Custom handler? if (eh.customcaller == null) { ret = eh.handler(eh.that, args); } else { //Allow them to call it ret = eh.customcaller(eh, true, eh.that, args); } //Got our loot, return with it return(ret); } } return(null); } }
/// <summary> /// Triggers an event /// </summary> static public void trigger(IEventObject obj, string name, bool bCautious, params object[] args) { //Attempt to hold the sync object bool bSync = true; if (bCautious) { bSync = DdMonitor.TryEnter(obj._sync); } else { DdMonitor.Enter(obj._sync); } //If this would block, queue the request in the threadpool if (!bSync) { //Queue! ThreadPool.QueueUserWorkItem( delegate(object state) { //Reattempt the trigger trigger(obj, name, false, args); } ); //Done return; } try { //Does the event exist? HandlerList list; if (!obj.events.TryGetValue(name, out list)) { //No? No need to worry return; } //Got it! Execute all handlers foreach (CEventHandler eh in list.methods) { //Sanity checks if (eh.argnum != args.Length) { throw new ArgumentException("Parameter mismatch while attempting to invoke custom event '" + name + "'"); } //Use the logger if necessary using (LogAssume.Assume(obj._eventLogger)) { //We're ok, time to invoke it //Custom handler? if (eh.customcaller == null) { eh.handler(eh.that, args); } else { //Allow them to call it eh.customcaller(eh, false, eh.that, args); } } } } catch (Exception ex) { //Log Log.write(TLog.Exception, "Exception occured while triggering event (" + name + "):\r\n" + ex.ToString()); } finally { DdMonitor.Exit(obj._sync); } }
/// <summary> /// Makes a note of all unhandled exceptions /// </summary> public static void onException(object o, UnhandledExceptionEventArgs e) { //Talk about the exception using (LogAssume.Assume(server._logger)) Log.write(TLog.Exception, "Unhandled exception:\r\n" + e.ExceptionObject.ToString()); }