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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:34:55+00:00 2026-06-02T02:34:55+00:00

I am having a problem with using the ManualResetEvent class with a timeout parameter.

  • 0

I am having a problem with using the ManualResetEvent class with a timeout parameter. This problem occurs specifically on the WinXP embedded platform. The code works perfectly on other windows platforms. I am communicating with a TCP server. In my client code, I connect to the server and spawn a new thread wose job is to continuously monitor the receive socket for data. I send the data in the main thread. The code snippet is attached below :

internal void initSocket()
{
   .....
   .....
   if (socket.Connected)
   {
      Tracing.info("Connected to server");
      ReceiveThread = new Thread(new    ThreadStart(StartReceiving));
      ReceiveThread.Start();
    }
}

/// <summary>
/// Sends a request to Server and waits for its response.
/// </summary>
/// <param name="msg"></param>
/// <param name="timeout">Timeout time, when </param>
/// <returns></returns>
private CdcMessage sendSync(CdcMessage msg, int timeout)
{
    resultMessage = null;

    // store current messageId...
    resultMessagePackageId = msg.MessageId;

    String msgToSend = msg.serialize();

    Tracing.debug("SEND    : >> " + msgToSend);
    socketWriter.WriteLine(msgToSend);

    // Wait for response from read thread...
    resultReceivedEvent = new  ManualResetEvent(false);
    bool bReponseSent = resultReceivedEvent.WaitOne(timeout);

    if (!bReponseSent)
    {
        resultMessage = null;
    }

    return resultMessage;

}

/// <summary>
/// Thread function which continuously checks for the 
/// data from server. It will read the data only if it
/// is available
/// </summary>
public void StartReceiving()
{
    while (Connected)
    {
        try
        {
            Thread.Sleep(100);

            String response = socketReader.ReadLine();

            Tracing.info("Raw data received = " + response);

            resultMessage = CdcMessage.deserialize(response);

            Tracing.info("Deserialized response =  " + resultMessage);

            if (resultMessage == null)
            {
                continue;
            }
            else if (resultMessage.IsHeartbeat)
            {
                Tracing.debug("Heartbeat");
                socketWriter.WriteLine(response);
            }
            else if (!resultMessage.MessageId.Equals(resultMessagePackageId))
            {
                // not the correct package id...reject...
                Tracing.warn("REJECTED: Package-ID: " + resultMessage.MessageId);
                continue;
            }
            else
            {
                resultReceivedEvent.Set();
                Tracing.info("StartReceiving() : Received data");
                Tracing.debug("RECEIVED: >> " + response);
            }
        }
        catch (NullReferenceException nre)
        {
            Tracing.error("StartReceiving(): Socket doesn't exist!", nre);
            close();
            break;
        }
        catch (ObjectDisposedException ode)
        {
            Tracing.error("StartReceiving(): Socket is disposed!", ode);
            close();
            break;
        }
        catch (IOException ex)
        {
            Tracing.error("StartReceiving(): Socket IO-Exception!", ex);
            close();
            break;
        }
    }
}

I have highlighted the important aspects of code. It is observed that WaitOne(timeout) function works without any problems on most of the windows OS’s. But on XP embedded, I observe a problem. The WaitOne almost returns immediately with no data received from the receive thread.

What I did was I made the timeout as INFINITE by passing -1 to WaitOne. In this case, I could solve the problem. But this creates other side effects (e.g. if the server was shutdown, then WaitOne never returns!)

Can someone please help me in solving this issue ?

  • 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-02T02:34:57+00:00Added an answer on June 2, 2026 at 2:34 am

    I’m not sure I understand your code correctly, but the lines

    socketWriter.WriteLine(msgToSend);
    resultReceivedEvent = new  ManualResetEvent(false);
    bool bReponseSent = resultReceivedEvent.WaitOne(timeout);
    

    look strange to me. I think this would be better:

    resultReceivedEvent.Reset();
    socketWriter.WriteLine(msgToSend);
    bool bReponseSent = resultReceivedEvent.WaitOne(timeout);
    

    There may be a potential race condition if an old ManualResetEvent gets set before the new one is created. There doesn’t seem to be a reason to create a new instance of ManualResetEvent here. Just call Reset on the old instance, and make sure you reset it before sending the message.

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

Sidebar

Related Questions

I am having a problem using the signedCMS.decode routine. See the code below. The
I'm having a bit of a problem using FieldOffset correctly with arrays. The code
I'm having a problem using nested partials with dynamic form builder code (from the
I am having a problem using $html->link helper in my view. Consider this snippet...
I'm having a problem using PyQT4 on a Debian server. My script works fine
I am having problem in using this font in my Application. I have imported
I'm having a problem using custom components at runtime. I have this custom FlowLayoutPanel
I am having a problem using the repaint method in the following code.Please suggest
I'm having a problem using the Boost library in Dev C++, specifically the regex
I'm having a problem using methods in my module's model class. I have a

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.