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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:44:41+00:00 2026-05-17T22:44:41+00:00

I have a problem returning an object that was created in other thread. The

  • 0

I have a problem returning an object that was created in other thread.
The situation is this, my app need to send some request to remote server and wait an answer.
In the main thread (1) i have a method called SendAndWait. This method put a message in a message queue for be sent and wait for an answer.
Another thread (2) send the messages in the queue.
A third thread (3) recieve messages and return to the main thread (1) the message with their answer.

This is my code.

Thread1:

void SendAndWait()
{
    AutoResetEvent waitForAnswer = new AutoResetEvent(false);
    int msgReferenceNumber = 1;
    MyMessage msg;
    
    // Register to wait for answer to the message with the number = msgReferenceNumber
    RegisterToWait(msgNumber, waitForAnswer, ref msg);
    // Send the request to the server, with the reference number.
    SendRequest(msgReferenceNumber);
    
    // Wait until the server send my answer
    waitForAnswer.WaitOne();

    // Now I have my  answer. BUT I HAVE msg == null
    MessageBox.Show(msg.Text);
}

Thread 2:

public class WhoWait
{
    public int RefNumMsg;
    public AutoResetEvent WaitForAnswer;
    public MyMessage MessageWithAnswer;
}

// In this Dictionary I save who wait for answers.
private Dictionary<int, WhoWait> Waiting = new Dictionary<WhoWait>();

void RegisterToWait(int msgRefNumber, AutoResetEvent waitForAnswer, ref msgWithAnswer)
{
  WhoWait w = new WhoWait();
  w.RefNumMsg = msgRefNumber;
  w.WaitForAnswer = waitForAnswer;
  w.MessageWithAnswer = msgWithAnswer;

  Waiting.Add(msgRefNumber, w);
}

Thread3:

void OnRecieve()
{
     // Get data from server
     MyMessage msg = GetMessageData();

     // Search for someone waiting for this answer
     WhoWait w = Wainting[msg.RefNum];

     // Set the response message
     w.MessageWithAnswer = msg;

     // Warn to the main thread. Message with their answer arrive
     w.WaitForAnswer.Set();
}

The whole thing is working, except for the Message reference.
When I read the object with I pass wich the ref key I have a null.

Can someone help me?

Thanks in advance.

  • 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-17T22:44:42+00:00Added an answer on May 17, 2026 at 10:44 pm

    I solve this adding a class who keep the object (the OneMessageIn class).

    // This class keep the copy of the message when arrived
    public class OneMessageIn
    {
        public MyMessage Message
    }
    
    void SendAndWait() 
    { 
        AutoResetEvent waitForAnswer = new AutoResetEvent(false); 
        int msgReferenceNumber = 1; 
        OneMenssageIn msgBox = new OneMessageIn(); 
     
        // Register to wait for answer to the message with the number = msgReferenceNumber 
        RegisterToWait(msgNumber, waitForAnswer, ref msgBox); 
        // Send the request to the server, with the reference number. 
        SendRequest(msgReferenceNumber); 
     
        // Wait until the server send my answer 
        waitForAnswer.WaitOne(); 
     
        // Now I have my answer!!!
        MessageBox.Show(msgBox.Message.Text); 
    }
    
    public class WhoWait     
    {     
        public int RefNumMsg;     
        public AutoResetEvent WaitForAnswer;     
        public OneMessageIn MessageWithAnswer;     
    }     
    // In this Dictionary I save who wait for answers.     
    private Dictionary<int, WhoWait> Waiting = new Dictionary<WhoWait>();     
    
    // I'm not sure if ref is necesary, but with it works. I don't try without ref.    
    void RegisterToWait(int msgRefNumber, AutoResetEvent waitForAnswer, ref OneMessageIn msgBox)    
    {    
      WhoWait w = new WhoWait();    
      w.RefNumMsg = msgRefNumber;    
      w.WaitForAnswer = waitForAnswer;    
      w.MessageWithAnswer = msgBox;    
        
      Waiting.Add(msgRefNumber, w);    
    }
    
    void OnRecieve()       
    {       
         // Get data from server       
         MyMessage msg = GetMessageData();       
           
         // Search for someone waiting for this answer       
         WhoWait w = Wainting[msg.RefNum];       
           
         // Set the response message       
         w.MessageWithAnswer.Message = msg;       
           
         // Warn to the main thread. Message with their answer arrive       
         w.WaitForAnswer.Set();       
    } 
    

    This solution works great for me.

    Thanks all who help me with this problem.

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

Sidebar

Related Questions

I have problem in some JavaScript that I am writing where the Switch statement
I enjoy developing algorithms using the STL, however, I have this recurring problem where
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem when I try insert some data to Informix TEXT column via
I have problem with return statment >.< I want to store all magazine names
I have problem with starting processes in impersonated context in ASP.NET 2.0. I am
I have problem with ActionLink. I'd like to pass to my ActionLink parameter for
I do not have problem as such but I am quite new to Ruby.
I have a problem using the Java search function in Eclipse on a particular
I have a problem with a little .Net web application which uses the Amazon

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.