Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to implement Control.Invoke analog in custom type?
    primarykey
    data
    text
    <p>I need to implement in my class "<em>Invoke()</em>" method with the same behavior as <em>Control.Invoke()</em> has. </p> <p>So when I am working with the instance of my <em>InvokableEntity</em> class from a thread different from thread where instance was created, I will be able to call <em>invokableEntity.Invoke(<strong>delegate</strong>)</em> and <strong>delegate</strong> will be executed in the context of the thread instance of InvokableEntity was created in.</p> <p>And yes, I've read <a href="https://stackoverflow.com/questions/1739283/c-multithreading-invoke-without-a-control">this</a> question, it doesn't helped me =(</p> <p>Please take a look at his code, it illustrates my tries to implement described behavior for event handler (<strong>CustomProcessor_ProgressChanged</strong> method should be executed from thread where it was subscribed to the event, but I can't do this):</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.ComponentModel; using System.Windows.Forms; namespace MultiThread { class Program { private static CustomProcessor customProcessor = new CustomProcessor(); static void Main(string[] args) { Console.WriteLine("Worker was run from thread: {0}", Thread.CurrentThread.ManagedThreadId); customProcessor.ProgressChanged += new EventHandler(CustomProcessor_ProgressChanged); Thread workerThread = new Thread(customProcessor.Process); AsyncOperation asyncOperation = AsyncOperationManager.CreateOperation(null); //SynchronizationContext context = SynchronizationContext.Current; workerThread.Start(asyncOperation); Console.ReadLine(); } static void CustomProcessor_ProgressChanged(object sender, EventArgs e) { Console.WriteLine("Custom ProgressChanged was handled in thread: {0}", Thread.CurrentThread.ManagedThreadId); } } class CustomProcessor { public event EventHandler ProgressChanged; public void RaiseProcessChanged(object o) { Console.WriteLine("RaiseProgressChanged was handled in thread: {0}", Thread.CurrentThread.ManagedThreadId); if (this.ProgressChanged != null) { this.ProgressChanged(this, EventArgs.Empty); } } public void Process(object asyncOperation) { Console.WriteLine("CustomProcessor.Process method was executed in thread: {0}", Thread.CurrentThread.ManagedThreadId); AsyncOperation asyncOperationInternal = (AsyncOperation)asyncOperation; asyncOperationInternal.Post(this.RaiseProcessChanged, null); //SynchronizationContext context = (SynchronizationContext) asyncOperation; //context.Send(s =&gt; this.RaiseProcessChanged(null), null); //this.RaiseProcessChanged(new object()); } } } </code></pre> <p>Thanks!</p>
    singulars
    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.
 

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