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

  • Home
  • SEARCH
  • 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 8701057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:18:16+00:00 2026-06-13T02:18:16+00:00

I am working on a serial port related application. While using DataReceived event of

  • 0

I am working on a serial port related application. While using DataReceived event of SerialPort I need to update a textbox with the received bytes:

private void Connection_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    ledReceive.On = true;

    var data = Connection.ReadExisting();

    _readBuffer.Add(data);
    Invoke(new EventHandler(AddReceivedPacketToTextBox));       
}

So I use Invoke to update the textbox. But there is a big problem. When I try to close connection, my UI gets freezed, I think this is becase Invoke is doing somehing perhaps.

A friend said I should use RequiredInvoke, but I have no idea what he ment really. How can I close the connection without messing up the invoke and UI thread?

Here is my close method:

private void DisconnectFromSerialPort()
{
    if (Connection != null && Connection.IsOpen)
    {
        Connection.Close();
        _receivedPackets = 0; //reset received packet count
    }
}
  • 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-13T02:18:17+00:00Added an answer on June 13, 2026 at 2:18 am

    I’m not fully sure, but I would guess that the thread that is updating your UI is removed when you close your connection, which could explain why it freeze. But I can’t be sure without digging in all your code.

    InvokeRequired return true if the thread trying to access your element isn’t native to your UI. Or if you prefer MS explaination:

        // Summary:
        //     Gets a value indicating whether the caller must call an invoke method when
        //     making method calls to the control because the caller is on a different thread
        //     than the one the control was created on.
        //
        // Returns:
        //     true if the control's System.Windows.Forms.Control.Handle was created on
        //     a different thread than the calling thread (indicating that you must make
        //     calls to the control through an invoke method); otherwise, false.
    

    And a basic implementation:

        public void ReceiveCall()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new CallDelegate(ReceiveCall), new object[] { });
                return; // Important to not continue on this method.
            }
    
            // Whatever else this function is suppose to do when on the correct thread.
        }
    

    The Invoke should be the first thing your function does, to prevent that thread from entering deep in your UI. Now, if the connection itself is determined to be within the bounderies of your UI’s thread, it probably won’t trigger “InvokeRequired”. One way to prevent this from happening, would be to manually create a thread that update your UI that isn’t bound to your connection.

    I had a similar problem with a clock updating the rendering of multiple UI element. The clock would work fine, but would “hang” until all the UI is done rendering, which in case of a lot of UI would mean it would skip it’s next tick.

         Thread updateUI = new Thread(updateUIDelegate);
         updateUI.Start();
    

    This way, my clock (or your connection) would start an independant thread that does its stuff on its own. You may also want to check the Monitor class or the lock keyword to prevent colliding threads or multiple threads using the same variables.

    EDIT: Or you could read the awesome answer at, which probably make more sense than mine: What causes my UI to freeze when closing a serial port?

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

Sidebar

Related Questions

I am working on a serial port related application. While using DataReceived event of
I need to be able to write signed bytes to a serial port using
I am reading data from a scale using a serial port. Communication is working
I am working on serial port application on Linux , when select () api
I am working on a web controlled rover and am using a serial port
I'm working on an application that connects to a serial port. I have experience
I am working on a project with serial port. I want to Write command
I am working on a multi threaded server application for processing serial/USB ports. The
I'm working on an embedded system and it uses one serial port for all
I am working on robot which has to control using wireless serial communication. The

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.