WinXP嵌入式上的ManualResetEvent问题

时间:2012-04-17 14:21:38

标签: c# .net windows-7 windows-xp-embedded manualresetevent

我在使用带有timeout参数的ManualResetEvent类时遇到问题。此问题特别发生在WinXP嵌入式平台上。该代码在其他Windows平台上运行良好。我正在与TCP服务器通信。在我的客户端代码中,我连接到服务器并生成一个新的线程wose作业是持续监视接收套接字的数据。我在主线程中发送数据。代码段附在下方:

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;
        }
    }
}

我强调了代码的重要方面。据观察,WaitOne(超时)功能在大多数Windows操作系统上都没有任何问题。但是在嵌入式XP上,我发现了一个问题。 WaitOne几乎立即返回,没有从接收线程收到数据。

我做的是通过将-1传递给WaitOne,将超时设置为INFINITE。在这种情况下,我可以解决问题。但这会产生其他副作用(例如,如果服务器关闭,那么WaitOne永远不会返回!)

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

我不确定我是否正确理解了您的代码,但行

socketWriter.WriteLine(msgToSend);
resultReceivedEvent = new  ManualResetEvent(false);
bool bReponseSent = resultReceivedEvent.WaitOne(timeout);
对我来说很奇怪。我认为这会更好:

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

如果在创建新ManualResetEvent之前设置了旧ManualResetEvent,则可能存在潜在竞争条件。似乎没有理由在此处创建Reset的新实例。只需在旧实例上调用{{1}},并确保在发送邮件之前重置它。