Why app is still working, when i close it.
I guess it is caused by reading of data from serial port.
Serial Port number is choosed from ComboBox.
Function WriteData update checkboxes depending on data from serial port.
Here’s extract:
// 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) { ... }
Your code is very likely to cause deadlock, blocking your program on the Close() call. The problem statement is the Dispatcher.Invoke() call. That call cannot complete until the UI thread has dispatched the call. The deadlock occurs when you call Close() and at the same time the DataReceived event is busy executing. The Close() call cannot complete because the event is running. The event handler cannot complete because Invoke() cannot complete because the UI thread is not idle, it is stuck in the Close() call. Deadlock city.
This is especially likely to happen in your code because it has a bug. You call DiscardInBuffer() in DataReceived. That throws away the received data so the next ReadChar() call is going to block for a while, waiting for some more data to get received, possibly forever if the device isn’t sending anything anymore.
Fix this problem by deleting the DiscardInBuffer() call and by using Dispatcher.BeginInvoke() instead.