void InvokeEvent(params object[] par) { Console.WriteLine("Remoting event received: " + realDelegate.Method.Name); EventForwarderEventArgs args = new EventForwarderEventArgs(realDelegate, par); if (EventReceived != null) { EventReceived(this, args); } // Invoke event if it was not already invoked if (!args.WasInvoked) { if (Application.OpenForms.Count > 0) { Application.OpenForms[0].Invoke(new EventHandler(delegate { args.InvokeEvent(); })); } else { args.InvokeEvent(); } } }
// This function can be called at any time any number of times static void OnEventReceived(object sender, EventForwarderEventArgs args) { lock (storeOneEventLock) // Store just one event at a time { while ((messageLoopsSTA.Count > 0) && // While there is someone who can invoke us (args.WasInvoked == false)) // and while the event was not invoked yet { // Store event storedEvent = args; // Pump all STA message loops lock (messageLoopsSTA) { foreach (EventWaitHandle loop in messageLoopsSTA) { loop.Set(); } } // Wait until we are told to check conditions again recheckEventStatus.WaitOne(); } } }