Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 713851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:00:29+00:00 2026-05-14T05:00:29+00:00

I need to implement in my class Invoke() method with the same behavior as

  • 0

I need to implement in my class “Invoke()” method with the same behavior as Control.Invoke() has.

So when I am working with the instance of my InvokableEntity class from a thread different from thread where instance was created, I will be able to call invokableEntity.Invoke(delegate) and delegate will be executed in the context of the thread instance of InvokableEntity was created in.

And yes, I’ve read this question, it doesn’t helped me =(

Please take a look at his code, it illustrates my tries to implement described behavior for event handler (CustomProcessor_ProgressChanged method should be executed from thread where it was subscribed to the event, but I can’t do this):

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 => this.RaiseProcessChanged(null), null);

            //this.RaiseProcessChanged(new object());
        }
    }
}

Thanks!

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:00:30+00:00Added an answer on May 14, 2026 at 5:00 am

    Control.Invoke() uses the PostMessage() API call to post a message that will be consumed by the message pump of the main GUI thread.

    Let’s pretend that you create an instance of CustomProcessor in Thread#1 that is not a GUI thread and after creating an instance of CustomProcessor, Thread#1 goes on with a long processing operation. If you need to Invoke an operation on Thread#1, you do not want to abort the current operation, it is a better idea instead to queue a workitem that will be consumed by Thread#1 each time Thread#1 finishes a task.
    If there is no logic that enqueues new work from other threads and dequeues and processes it within Thread#1, it will not magically work out-of-the-box.

    If you need this functionality on multiple threads that do not have a message pump, with a custom type that does not derive from Control, a message queue or equivalent might be implemented. This way, however the creating thread will be spending its life in the message/work queue loop, most of the time waiting for new work – just like the main GUI thread in a Windows Forms application when you call Application.Run(). This might not be what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to implement a class called Foo where I need to Get and
For a class assignment I need to implement a destructor for the objects of
I need to implement own TreeView with blinked TreeNode. My prototype is: public class
I have a parent and child class that both need to implement IDisposable .
class Foo(){ public List<string> SomeCollection; } I need to implement an event which can
I'm developing an application where I the need to invoke a method of a
I need to implement portable code, but I do not know how to deal
I need to implement an efficient excel-like app. I'm looking for a data structure
I need to implement the following: There is a table A which is supposed
i need to implement the email signature with image.As of now we only support

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.