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

  • Home
  • SEARCH
  • 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 8274383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:43:04+00:00 2026-06-08T07:43:04+00:00

I run a console application and declared and instantiated an object of type Foo

  • 0

I run a console application and declared and instantiated an object of type Foo. Besides the Main() method I want to specify an additional method (lets call it ProcessMessages()) and have it perform some work. The main job of Foo is to accept messages that are sent to a tcp socket and processed. As part of the process I need to specify a function func<T> to which such messages are passed. ProcessMessages() (outside of Foo) is supposed to be such function. How can I register this function with foo without having to pass reference to its containing class?

Foo contains a TPL data flow bock which requires a func<T> which operates on the queued items in the block. But the function is not contained within Foo but in the class that instantiated an object of type Foo. I am looking for the fastest way of later on calling this method even if I have to write more elaborate code. Ultimately I want to hide the underlying tpl data flow block within Foo and only want to call a function in Foo that sets up a tpl data flow block and registers with it a method that is located outside of Foo to which items can be passed later on. What is the best way to get this done? The registration part is the one which puzzles me.

EDIT: Here is some code that describes maybe better what I try to achieve:

a) In Main() I have the following method which is getting called from within a different class:

public void OnControlMessage(object sender, EventArgs args)
    {
        Console.WriteLine("Comp1 received Control Message");
    }

I register this Method in the following way:

List<ZeroMqMessaging.CallBackMethod> registeredMethods = new List<ZeroMqMessaging.CallBackMethod>(){ 
            new ZeroMqMessaging.CallBackMethod(MessageType.ControlMessage, OnControlMessage) };

b) The ActionBlock that invokes the callback looks as follows:

ActionBlock<Tuple<int, byte[]>> newActionBlock = new ActionBlock<Tuple<int, byte[]>>(x => callback.eventHandler(this, new MyEventArg(callback.messageType, x.Item2)));

OnControlMessage gets called correctly, however, I need to pass specific messages to OnControlMessage as part of EventArgs. I created a class MyEventArg that derives from EventArg but I still am not able to access members of MyEventArg in OnControlMessage.

Preferably I like to have OnControlMessage(ControlMessage message){…} in order to not to have to cast. How can I register this OnControlMessage so that I can call back this method with arguments of my choosing? I have potentially many different such callback methods so requirement is to stick to a 1-line short way to register as I currently do in a) when I create CallBackMethod objects. I hope this is somewhat clearer.

  • 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-06-08T07:43:06+00:00Added an answer on June 8, 2026 at 7:43 am

    You probably need to create a delegate:

    public delegate void MyMessageHandler(string message);
    

    You can then use it like this:

    public void Work(MyMessageHandler callback)
    {
        string message = "";
        ....
        callback(message);
    }
    

    You then call it like this:

    public Main()
    {
        Work(ProcessMessages);
    }
    public void ProcessMessages(string message)
    {
        ....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to run a console application (eg app.exe) from a windows form load
I want to make an console application of c which can run other applications
I have a console application written in C#/.NET that I want to run from
I want to run a console application that will output a file. I user
I've got a console application and I want to run it from another application.
When I develop a C# console application (which will run on a server) and
I need to schedule a console application to run daily at a certain time
I have to run a console application from my Windows Application. The console application
I'm using Delphi 2010, and I need to run a console application and automate
I run a console application which will reference lib.dll . In lib.dll, there is

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.