0

テスト機器を制御しようとしていますが、通信方法のシーケンスを正しくする必要があります。

まず私が電話しますStartGettingTraceData()。その後、しばらくして、関数を再起動するのではなくStopGettingTraceData()、関数を終了しようとします。GetTraceData()しかし、それは決して起こりません。実際、私はその行にたどり着くことはありDoneTraces.Set()ませbool timedOut = !DoneTraces.WaitOne(10000)timedOut

private static AutoResetEvent DoneTraces = new AutoResetEvent(false);

private void GetTraceData()
{
    byte[] receivedbytes = new byte[1];
    if (Connection.ReadData(receivedbytes) && receivedbytes[0] == 192)
        ProcessIncomingTrace();

    Thread.Sleep(100);

    if (RunTraceQueryWorker)
        new Thread(GetTraceData).Start();
    else
    {
        Thread.Sleep(200);
        DoneTraces.Set();
    }
}

private void StartGettingTraceData()
{
    RunTraceQueryWorker = true;
    new Thread(GetTraceData).Start();
}

private bool StopGettingTraceData()
{
    RunTraceQueryWorker = false;
    bool timedOut = !DoneTraces.WaitOne(10000);
    return timedOut;
}

何が起こっているのかについて何か考えはありますか?

編集:

これが私の Connection.ReadData(...) 関数です。ちなみにシリアル接続です。

public bool ReadData(byte[] responseBytes)
{
    int bytesExpected = responseBytes.Length, offset = 0, bytesRead;
    while (bytesExpected > 0 && (bytesRead = MySerialPort.Read(responseBytes, offset, bytesExpected)) > 0)
    {
        offset += bytesRead;
        bytesExpected -= bytesRead;
    }
    return bytesExpected == 0;
}
4

2 に答える 2

0

ReadData通話がブロックされている可能性があります。余談ですが、このような再帰的なスレッド処理を行うことで、自分自身に負担がかかっています...ループだけを使用することはできませんか?

private void GetTraceData()
{
    byte[] receivedbytes = new byte[1];

    while( RunTraceQueryWorker )
    {
        if( Connection.ReadData(receivedbytes) && receivedbytes[0] == 192 )
        {
            ProcessIncomingTrace();
        }
        Sleep(100);
    }

    Thread.Sleep(200);
    DoneTraces.Set();
}
于 2013-05-09T14:49:33.153 に答える