私は現在、C# の CAN テクノロジと、メッセージの受信と送信に関連して多くの開発を行っています。これまでのところ、私が送信するすべてのメッセージは消費者に正しく送信され、プロデューサーが送信するすべてのメッセージは消費者 (私) に正しく送信されます。
メッセージが受信 FIFO に到着するたびに、_messageReceivedEvent がトリガーされます。その後、1 つ以上の受信メッセージの処理が開始されます。メッセージ処理の後、既存のタイマー (別のスレッド) をリセットする必要があります。これは、5 秒以内にメッセージが到着しない場合、タイマー スレッドはリセットされず、経過した場合、タイマー スレッドは経過したことを通知し、1 つのパラメーター (_isDataCommunicationStillAlive) を「FALSE」に設定する必要があることを意味します。
Main-Thread (MessageReceiption-Backgroundworker を呼び出します):
private Timer _commTimer;
public void MessageReceiption()
{
_quitMessageObservation = false;
// Generate and call new DataCommunicationTimer as Backgroundworker
DataCommunicationTimer();
BackgroundWorker bgWorker = new BackgroundWorker();
bgWorker.DoWork += ObservingMessageReceiptionFifo;
bgWorker.RunWorkerCompleted += ObservingMessageReceiptionCompleted;
bgWorker.RunWorkerAsync();
}
// Observes, if a new message arrived in receiption-FIFO
public void ObservingMessageReceiptionFifo(object sender, DoWorkEventArgs e)
{
do
{
// If a new message arrives in my receiption-FIFO
if (_messageReceivedEvent.WaitOne(100, false))
{
/////// Here would be code, to handle received message(s) ///////
_isDataCommunicationStillAlive = true;
// After message receiption, resetting timer should begin.
// So if no new message arrives within 5 seconds, the
// timer counts down, and if elapsed, TimeElapsedEvent
// should trigger and do some more stuff, like setting
// a boolean parameter (_isDataCommunicationStillAlive)
// to 'FALSE'.
RunningDataCommTimer(sender, e);
}
} while (_quitMessageObservation == false);
e.Result = "MessageReceiptionTerminated";
}
private void ObservingMessageReceiptionCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Result.Equals("MessageReceiptionTerminated"))
{
Console.WriteLine("Observing message-receiption terminated.");
}
}
DataCommunicationTimer (Backgroundworker (MessageReceiption から呼び出される) として):
public void DataCommunicationTimer()
{
_commTimer = new Timer();
// Registers new TimeElapsedEvent
_commTimer.Elapsed += new ElapsedEventHandler(TimeElapsedEvent);
BackgroundWorker bgWorker = new BackgroundWorker();
bgWorker.DoWork += RunningDataCommTimer;
bgWorker.RunWorkerAsync();
}
public void RunningDataCommTimer(object sender, DoWorkEventArgs e)
{
while(_isConnected == true)
{
// Set the Interval to 5 seconds (5000 milliseconds).
_commTimer.Interval = 5000;
_commTimer.Enabled = true;
_commTimer.Start();
}
}
public void TimeElapsedEvent(object source, ElapsedEventArgs e)
{
_isDataCommunicationStillAlive = false;
_commTimer.Elapsed -= TimeElapsedEvent;
}
問題は、Timer-Backgroundworker が 1 秒以内に終了するため、動作が理解できないことです。別の問題は、私が知らないことですが、残り時間に関する特定のタイマーインスタンスを読み取る方法です...
これらのコード行に基づいて、バグを見つけることができません。:(((
手伝っていただけませんか?!