Note that there are some explanatory texts on larger screens.

plurals
  1. POFire Event on Background Thread
    primarykey
    data
    text
    <p>My apologies I was not accurate in my question and effort. I am developing Console application which has different components. Now I have decoupled them and want them to interact them using asynchronous Publisher/Subscriber way; similar to WPF. So in this case I will have one Master thread which will always be there and depending on request it will invoke event e.g. DataRequested which would be fired on background thread. Once Background thread completes process it will fire event again e.g. DataCompleted which should come back to the calling thread i.e. Master thread. I hope I am clear in my explanation.</p> <pre><code>So far I have coded below; where I have EventBroker. public class EventBroker { public static event EventHandler SubscriptionAdded; public static event EventHandler SubscriptionRemoved; private static volatile EventBroker instance; private static object syncRoot = new Object(); private static Dictionary&lt;string, List&lt;Delegate&gt;&gt; subscriptions; /// &lt;summary&gt; /// Initializes a new instance of the &lt;see cref="T:EventBroker"/&gt; class. /// &lt;/summary&gt; private EventBroker() { } /// &lt;summary&gt; /// Gets the instance. /// &lt;/summary&gt; /// &lt;value&gt;The instance.&lt;/value&gt; public static EventBroker Instance { get { if (instance == null) { lock (syncRoot) { if (instance == null) { instance = new EventBroker(); subscriptions = new Dictionary&lt;string, List&lt;Delegate&gt;&gt;(); } } } return instance; } } /// &lt;summary&gt; /// Gets or sets the internal subscriptions dictionary. /// &lt;/summary&gt; /// &lt;value&gt;The subscriptions.&lt;/value&gt; private static Dictionary&lt;string, List&lt;Delegate&gt;&gt; Subscriptions { get { return EventBroker.subscriptions; } set { lock (syncRoot) { EventBroker.subscriptions = value; } } } /// &lt;summary&gt; /// Raises the subscription added event. /// &lt;/summary&gt; /// &lt;param name="e"&gt;The &lt;see cref="T:System.EventArgs"/&gt; instance containing the event data.&lt;/param&gt; private static void OnSubscriptionAdded(EventArgs e) { if (SubscriptionAdded != null) SubscriptionAdded(instance, e); } /// &lt;summary&gt; /// Raises the subscription removed event. /// &lt;/summary&gt; /// &lt;param name="e"&gt;The &lt;see cref="T:System.EventArgs"/&gt; instance containing the event data.&lt;/param&gt; private static void OnSubscriptionRemoved(EventArgs e) { if (SubscriptionRemoved != null) SubscriptionRemoved(instance, e); } /// &lt;summary&gt; /// Subscribe method to the specified event. /// &lt;/summary&gt; /// &lt;param name="id"&gt;The id.&lt;/param&gt; /// &lt;param name="method"&gt;The method Delegate to be invoked when Event fires.&lt;/param&gt; public static void Subscribe(string id, Delegate method) { //Check if there is a existing event List&lt;Delegate&gt; delegates = null; if (Subscriptions == null) Subscriptions = new Dictionary&lt;string, List&lt;Delegate&gt;&gt;(); if (Subscriptions.ContainsKey(id)) { delegates = subscriptions[id]; } else { delegates = new List&lt;Delegate&gt;(); Subscriptions.Add(id, delegates); } delegates.Add(method); OnSubscriptionAdded(new EventArgs()); } /// &lt;summary&gt; /// Unsubscribe method from event notifications /// &lt;/summary&gt; /// &lt;param name="id"&gt;The id.&lt;/param&gt; /// &lt;param name="method"&gt;The method.&lt;/param&gt; public static void Unsubscribe(string id, Delegate method) { if (Subscriptions.ContainsKey(id)) { if (Subscriptions[id].Contains(method)) { Subscriptions[id].Remove(method); OnSubscriptionRemoved(new EventArgs()); } if (Subscriptions[id].Count == 0) Subscriptions.Remove(id); } } /// &lt;summary&gt; /// Fire the specified event by and pass parameters. /// &lt;/summary&gt; /// &lt;param name="id"&gt;The id.&lt;/param&gt; /// &lt;param name="args"&gt;The args.&lt;/param&gt; public static void Execute(string id, object sender, EventArgs e) { if (Subscriptions.ContainsKey(id)) { for (int i = 0; i &lt; Subscriptions[id].Count; i++) { Delegate x = Subscriptions[id][i]; DynamicInvoke(id, x, sender, e); if (!Subscriptions.ContainsKey(id)) break; } } } /// &lt;summary&gt; /// Checks to see if target of invocation is still a valid /// (non-disposed objects). Then it dinamicly invokes Delegate. /// &lt;/summary&gt; /// &lt;param name="id"&gt;Event ID&lt;/param&gt; /// &lt;param name="x"&gt;Delegate to invoke&lt;/param&gt; /// &lt;param name="args"&gt;Object array of arguments&lt;/param&gt; private static void DynamicInvoke(string id, Delegate x, object sender, EventArgs e) { if (x.Method != null) { if (x.Target is Control) { Control ctl = (Control)x.Target; if (ctl.IsDisposed) { Unsubscribe(id, x); return; } } if (x.Target == null) { Unsubscribe(id, x); return; } x.DynamicInvoke(sender, e); ***//this becomes blocking call untill EventHandle is completed and hangs Master Thread*** } } } </code></pre> <p>I use this EventBroker to keep track of my Subscribers and once Publisher comes I invoke certain delegate. But it gets invoked only on Master thread and it gets hanged. I want to invoke EventHandler on separate thread.</p> <pre><code> public class MasterClass { public MasterClass() { EventBroker.Subscribe("Topic2", new EventHandler&lt;EventArgs&lt;string&gt;&gt;(CallBackfromWorker)); } public void InvokeTest() { EventArgs&lt;string&gt; EventArgs = new EventArgs&lt;string&gt;("Test"); EventBroker.Execute("Topic1", null, EventArgs); //I want both of this to be asynchronous. } public void CallBackfromWorker(object sender, EventArgs&lt;string&gt; e) { Debug.Pring("Get Called Asynchronously from Worker thread through Event"); } } **//Worker Class** public class WorkerClass { public WorkerClass() { EventBroker.Subscribe("Topic1", new EventHandler&lt;EventArgs&lt;string&gt;&gt;(HandleRapRequest1)); } public void HandleRapRequest1(string RAPRequest) //public void HandleRapRequest1(object sender, EventArgs&lt;string&gt; e) { Logger.LogToDisplay("WorkerClass Request" + RAPRequest); Logger.LogToDisplay("AsyncClient : " + System.Threading.Thread.CurrentThread.IsBackground); Logger.LogToDisplay("AsyncClient : " + System.Threading.Thread.CurrentThread.ManagedThreadId); Logger.LogToDisplay("Going to Sleep"); System.Threading.Thread.Sleep(10000); ***//Hangs my Master Thread*** EventBroker.Execute("Topic2", null, EventArgs); //I want both of this to be asynchronous. } } </code></pre> <p>So bottom line is I am looking for Asynchronous Eventbased Publisher/Subscriber in Console application...similar to CAB event sin SCSF and in WPF...</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload