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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:13:14+00:00 2026-06-10T03:13:14+00:00

Part of my program uses an event handler for the receive data of my

  • 0

Part of my program uses an event handler for the receive data of my serial port. The idea is when data is received that the text received is then added to the textbox (rx). I did not used to have this problem but something has changed and I can’t figure out what. So now I am re-examining the way this is handled.

During the form load of my winform the last thing I do is

if (!serialPort1.IsOpen)
{
     serialPort1.Open();
     serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
}

Then I have the event handler

private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
     string indata1 = serialPort1.ReadExisting();
    // rx.Text = " "; accidentally posted this. it was from trial and error.  
     rx.AppendText(Environment.NewLine + indata1);
}

When I run the program it stops at the rx.AppendText(Environment.NewLine + indata1); and gives the error

invalidoperationexception was unhandled: Control “accessed from a
thread other than the thread it was created on.

From what I have been able to read suggests that I need to use invoke or BeginInvoke.

I have never had problems appending the text before so now I can’t understand why it’s a problem. Also from what I have been reading on invoking i just don’t understand it.

Can someone help me understand how to use the invoke instance for my situation? or perhaps show me another way of appending the text box?

  • 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-10T03:13:15+00:00Added an answer on June 10, 2026 at 3:13 am

    Usually the exception you’re seeing occurs when you run in debug mode, and if you run your application in release mode, you’re unlikely to see the exception.

    However, it is best to use invoke, as you have read. Something like this:

        private delegate void RefreshTextBox();
    
        private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) {
            //this event is raised in an event separate from UI thread, 
            //so InvokeRequired must be checked and Invoke called to update UI controls. 
            if (this.InvokeRequired) {
                RefreshTextBox d = new RefreshTextBox(RefreshTextBoxResults);
                Invoke(d);
            } else {
                RefreshTextBoxResults();
            }
        }
    
        private void RefreshTextBoxResults() {
            string indata1 = serialPort1.ReadExisting();
            rx.Text = " ";
            rx.AppendText(Environment.NewLine + indata1);
        }
    

    The first time you see this invoke stuff, it’s nearly impossible to follow, but take a close look and give it some time and it will make sense. Promise. 🙂

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

Sidebar

Related Questions

There are two large text files (Millions of lines) that my program uses. These
I have this little part of a program that uses a JComboBox to select
As part of the program that I am writing I need it to: Compare
I am writing a GLSL program as part of a plugin that runs inside
Basically I have a part of my program (it's for Vector solving) that is
I have a program that uses the GPU for performing certain computations. I can
A part of my program uses WinInet's caching function (e.g. FindFirstUrlCacheEntry, FindNextUrlCacheEntry) to go
I have a program written in VB.NET which stops a service that uses file
I've got a simple program that uses an NSTimer to play a sound every
I wrote a Python extension in C, and my python program uses that extension.

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.