Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7765663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:13:16+00:00 2026-06-01T15:13:16+00:00

Why app is still working, when i close it. I guess it is caused

  • 0

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) { ... }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:13:17+00:00Added an answer on June 1, 2026 at 3:13 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still working on the same app from the other question, I was going
I am still working with securing my web app. I decided to use PDO
I'm still working on my phone book app and I want to update a
we have a J2EE app on which we're still working. It runs on Oracle
i still have a problem to develop a convenient app to the user. because
I've cleared my app of leaks using the leaks tool, but I still notice
In the Google Maps app on iPhone, the toolbar in the buttom is still
I've been experimenting with making Facebook apps and I'm still not clear on app
I just realized that my app, with over 300 users still using an Android
I know how to use locks in my app, but there still few things

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.