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 6105803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:00:38+00:00 2026-05-23T14:00:38+00:00

I have a class Communicator that works in a background thread receiving data on

  • 0

I have a class Communicator that works in a background thread receiving data on a TCP port.

The Communicator has an event OnDataReceived which is of a EventHandler<DataReceivedEventArgs> type.

There is another class Consumer that contains a method subscribed to the Communicator.OnDataReceived event.

comm.OnDataReceived += consumer.PresentData;

The Consumer class is created within a Form constructor and then one of its methods is called on another thread. This method is an infinite loop, so it stays in that method during the application execution.

What I’d like to do is for the Communicator.OnDataReceived event to invoke the consumer.PresentData method on consumer’s thread.

Is that even nearly possible? And if it is, what kind of mechanisms (sync classes) should I use?

  • 1 1 Answer
  • 0 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-23T14:00:39+00:00Added an answer on May 23, 2026 at 2:00 pm

    Add this somewhere in your code: (I usually put this in a static helper class called ISynchronizedInvoke so I can call ISynchronizedInvoke.Invoke(…));

    public static void Invoke(ISynchronizeInvoke sync, Action action) {
        if (!sync.InvokeRequired) {
            action();
        }
        else {
            object[] args = new object[] { };
            sync.Invoke(action, args);
        }
    }
    

    Then inside OnDataReceived, you could do:

    Invoke(consumer, () => consumer.PresentData());
    

    This invokes ‘consumer.PresentData’ on ‘consumer’.

    As for your design issue (consumer references communicator), you could introduce a method inside communicator such as:

    class Communicator {
        private ISynchronizeInvoke sync;
        private Action syncAction;
    
        public void SetSync(ISynchronizeInvoke sync, Action action) {
            this.sync = sync;
            this.syncAction = action;
        }
    
        protected virtual void OnDataReceived(...) {
            if (!sync.InvokeRequired) {
                syncAction();
            }
            else {
                object[] args = new object[] { };
                sync.Invoke(action, args);
            }
        }
    }
    

    This would give you a way to pass in the ISynchronizedInvoke from your consumer class. So you would be creating the ISynchronizedInvoke in the consumer assembly.

    class Consumer {
        public void Foo() {
            communicator.SetSync(this, () => this.PresentData());
        }
    }
    

    So basically you are creating everything you need to do the invoke, and just passing it in to your communicator. This resolves your necessity to have an instance or reference to consumer in communicator.

    Also note that I did not test any of this I am doing this all in theory, but it should work nicely.

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

Sidebar

Related Questions

I have class method that returns a list of employees that I can iterate
I have class with a member function that takes a default argument. struct Class
Background I'm writing an part of my app that has no UI. It sits
I have a class that communicates with a server (let's call it 'Downloader'). Sometimes,
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
I have class A: public class ClassA<T> Class B derives from A: public class
I have class with internal property: internal virtual StateEnum EnrolmentState { get { ..getter
I have class Cab(models.Model): name = models.CharField( max_length=20 ) descr = models.CharField( max_length=2000 )
I have: class Car {..} class Other{ List<T> GetAll(){..} } I want to do:
If I have class ObjA { public ObjB B; } class ObjB { public

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.