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 8710511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:38:38+00:00 2026-06-13T04:38:38+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)
{
    var data = Connection.ReadExisting();
    _readBuffer.Add(indata);

    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();            
    }
}

UPDATE

As Hans said I changed Invoke to BeginInvoke but now its a bit worse, my application stops working due to InvalidOperationException because the collection _readBuffer was modified (Thats what the detail says in VS)

Here is my code for adding text to textbox:

private void AddReceivedPacketToTextBox(object sender, EventArgs e)
{

    foreach (var i in _readBuffer)
        tbIn.Text += string.Format("{0:X2} ", i);

    tbIn.Text += Environment.NewLine;

    ScrollToBottom(tbIn);

    label4.Text = _receivedPackets.ToString();
    _receivedPackets++;

    _readBuffer.Clear(); //Possibly because clearing collection gets out of sync with BeginInvoke??         
}

2nd Update

I still have the problem, changing the Invoke() to BeginInvoke didn;t help. I also tried to add disconnect to form closing event nu success…anytime I close my form it gets stock (I mean its parent form, because this form that has access to serialport is being called from another form`.

I mean I figured out that the UI gets locked only in 2 cases: If I clock a button which calls Connection.Close() also if I try to close the form, the parent form will throw exception that some objects are disposed.

I call the serial form like this from the parent form:

public DebugForm DebugForm;

private void button1_Click(object sender, EventArgs e)
{
    if (DebugForm != null)
    {
        DebugForm.BringToFront();
        return;
    }

    DebugForm = new DebugForm();
    DebugForm.StartPosition = FormStartPosition.CenterScreen;
    DebugForm.Closed += delegate
                             {
                                 WindowState = FormWindowState.Normal;
                                 DebugForm = null;
                             };

    DebugForm.Show();
    WindowState = FormWindowState.Minimized; 
}

Could this be the problem?!

  • 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-13T04:38:39+00:00Added an answer on June 13, 2026 at 4:38 am

    The problem could be solved by adding a timer:

      bool formClosing = false;
        private void Connection_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
          if (formClosing) return;
          _buffer = Connection.ReadExisting();
          Invoke(new EventHandler(AddReceivedPacketToTextBox));
        }
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
          base.OnFormClosing(e);
          if (formClosing) return;
          e.Cancel = true;
          Timer tmr = new Timer();
          tmr.Tick += Tmr_Tick;
          tmr.Start();
          formClosing = true;
        }
        void Tmr_Tick(object sender, EventArgs e)
        {
          ((Timer)sender).Stop();
          this.Close();
        }
    

    Thanks to JohnWein from MSDN

    • 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.