なぜアプリはまだ閉じているのですか?
私はそれがシリアルポートからのデータの読み込みによって引き起こされると思います。
ComboBoxからシリアルポート番号を選択します。
機能シリアルポートからのデータに応じて、WriteDataアップデートのチェックボックスをオンにします。ここ
はエキスです:アプリが終了しない
// Choosing of communication port from ComboBox
private void comboBoxCommunication_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (serialPort.IsOpen)
{
serialPort.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(Recieve);
serialPort.Close();
}
try
{
ComboBoxItem cbi = (ComboBoxItem)comboBoxKomunikacia.SelectedItem;
portCommunication = cbi.Content.ToString();
serialPort.PortName = portCommunication;
serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Recieve);
serialPort.BaudRate = 2400;
serialPort.Open();
serialPort.DiscardInBuffer();
}
catch (IOException ex)
{
MessageBox.Show(ex.ToString(), "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
// Close the window
private void Window_Closed(object sender, EventArgs e)
{
if (serialPort.IsOpen)
{
serialPort.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(Recieve);
serialPort.Close();
}
}
// Data reading
private delegate void UpdateUiTextDelegate(char text);
private void Recieve(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
if (serialPort.IsOpen)
{
try
{
serialPort.DiscardInBuffer();
char c = (char)serialPort.ReadChar();
Dispatcher.Invoke(DispatcherPriority.Send,
new UpdateUiTextDelegate(WriteData), c);
}
catch(IOException ex)
{
MessageBox.Show(ex.ToString(), "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
// Update of checkboxes
private void WriteData(char c) { ... }
他のスレッド? Closedイベントが期待どおりに実行されていることを確認できますか(デバッガ/ロガー)? –
'serialPort'オブジェクトが使い捨てであり(' SerialPort'がそうだと思いますが)、このコードで正しく処分していない可能性があります。変数のスコープがより制御され、 'using'ブロックでラップすることができるように、変数を再構成することができます。 – David
@Henk Holterman - 他のスレッドはありません。私はシリアルポートからデータを読み始めるときにのみ、アプリケーションが正常に閉じて、それは正しく閉じられていません。 –