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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:35:11+00:00 2026-05-14T19:35:11+00:00

I have a Sender class that sends a Message on a IChannel : public

  • 0

I have a Sender class that sends a Message on a IChannel:

public class MessageEventArgs : EventArgs {
  public Message Message { get; private set; }
  public MessageEventArgs(Message m) { Message = m; }
}

public interface IChannel {
  public event EventHandler<MessageEventArgs> MessageReceived;
  void Send(Message m);
}

public class Sender {
  public const int MaxWaitInMs = 5000;
  private IChannel _c = ...;

  public Message Send(Message m) {
    _c.Send(m);
    // wait for MaxWaitInMs to get an event from _c.MessageReceived
    // return the message or null if no message was received in response
  }
}

When we send messages, the IChannel sometimes gives a response depending on what kind of Message was sent by raising the MessageReceived event. The event arguments contain the message of interest.

I want Sender.Send() method to wait for a short time to see if this event is raised. If so, I’ll return its MessageEventArgs.Message property. If not, I return a null Message.

How can I wait in this way? I’d prefer not to have do the threading legwork with ManualResetEvents and such, so sticking to regular events would be optimal for me.

  • 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-14T19:35:12+00:00Added an answer on May 14, 2026 at 7:35 pm

    Use a AutoResetEvent.

    Gimme a few minutes and I’ll throw together a sample.

    Here it is:

    public class Sender
    {
        public static readonly TimeSpan MaxWait = TimeSpan.FromMilliseconds(5000);
    
        private IChannel _c;
        private AutoResetEvent _messageReceived;
    
        public Sender()
        {
            // initialize _c
            this._messageReceived = new AutoResetEvent(false);
            this._c.MessageReceived += this.MessageReceived;
        }
    
        public Message Send(Message m)
        {
            this._c.Send(m);
            // wait for MaxWaitInMs to get an event from _c.MessageReceived
            // return the message or null if no message was received in response
    
    
            // This will wait for up to 5000 ms, then throw an exception.
            this._messageReceived.WaitOne(MaxWait);
    
            return null;
        }
    
        public void MessageReceived(object sender, MessageEventArgs e)
        {
            //Do whatever you need to do with the message
    
            this._messageReceived.Set();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Within my actor I have to create a class which sends a message to
I have this situation: interface MessageListener { void onMessageReceipt(Message message); } class MessageReceiver {
I have a IBAction such as: - (IBAction)showPicker:(id)sender; How can I get the name
I have a requirement of reading subject, sender address and message body of new
Is there a practical way to get the sender of a message in Smalltalk
I have a SMTP server that only accepts a predefined From sender. However, I
I have to pass parameters between two rails apps. In one side (sender) I
(resolved: see bottom) I have the following code snippet: Protected Sub SqlDataSource1_Inserted(ByVal sender As
I'm a newbie to python and the app engine. I have this code that
I have 4 buttons on main screen, each one sends me to a viewController.

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.