次のコードでバックグラウンドワーカーを呼び出しています。
private void UpdateDataTimer_Tick(object sender, EventArgs e)
{
if (!serialPortWorker.IsBusy)
{
serialPortWorker.RunWorkerAsync();
}
}
私のDoWork
イベントは次のとおりです。
private void serialPortWorker_DoWork(object sender, DoWorkEventArgs e)
{
//Configures serial port
connection.BaudRate = 19200;
connection.DataReceived += new SerialDataReceivedEventHandler(DataReceivedEvent);
//Sends the commands for opening diagnostics
string[] init_commands = { "STRING", "STRING", "STRING", "STRING", "STRING" };
foreach (string command in init_commands)
{
connection.WriteLine(command + connection.NewLine);
Thread.Sleep(1000);
}
const string constant_message_section = "G03";
string[] command_list = { "62", "64", "5C" };
//Writes all commands to all radio addresses
foreach (int address in radioAddresses)
{
foreach (string command in command_list)
{
for (int i = 0; i < MAX_ATTEMPTS; i++)
{
connection.WriteLine(constant_message_section + address.ToString("X4") + command);
Thread.Sleep(500);
}
}
}
Thread.Sleep(1000); //Give a little time for all responses to come in
}
何らかの理由で、UpdateDataTimer_Tick
イベントの数百回の呼び出しの後、それはそれ以上実行されませんserialPortWorker
。にデバッガを配置しましたが、まだビジーif (!serialPortWorker.IsBusy)
であることがわかりました。イベントserialPortWorker
のどこかでぶら下がっているはずですよね?DoWork
なぜ何かアイデアはありますか?
興味のある方のために、データ受信イベントは次のとおりです。
public void DataReceivedEvent(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string receive = sp.ReadLine();
try
{
Debug.Logger.WriteToDebug("Data Received Serial Port: " + receive);
}
catch { }
try
{
int unit_address = Int32.Parse(receive.Substring(1, 4), System.Globalization.NumberStyles.HexNumber);
if (radioAddresses.Contains(unit_address))
{
int radio_index = radioAddresses.IndexOf(unit_address) + 1;
int max_index = radio_index * 3;
integrityMonitor[radio_index] = DateTime.Now; //Last updated time
int message_data = 0;
if (receive.Contains("66"))
{
//Stuff
}
else if (receive.Contains("61"))
{
//Stuff
}
else if (receive.Contains("55"))
{
//Stuff
}
}
}
catch { }
}