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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:19:12+00:00 2026-06-15T09:19:12+00:00

I got global variable SerialPort comm; After opening com port im reading recived bytes

  • 0

I got global variable SerialPort comm; After opening com port im reading recived bytes and i get exception that com port is closed..
How can i access to it in backgroundworker correctly?
are there better way to declare comm?

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;
    while(true)
    {
        if ((worker.CancellationPending == true))
        {
            e.Cancel = true;
            break;
        }
        else
        {
            string str = comm.ReadLine();
            //...
        }
    }
}

EDIT: yea … just use this http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived.aspx
no need for BackgroundWorker

  • 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-15T09:19:12+00:00Added an answer on June 15, 2026 at 9:19 am

    SerialPort.ReadLine() is a blocking call. It won’t return until the port has received a line of text and a NewLine. Which inevitably means that your test for CancellationPending will not work, the code is stuck in the ReadLine() call. So you’ll call the bgw’s CancelAsync() call and then close the serial port. And that causes the ReadLine() method to throw an exception.

    There are no good ways to do this cleanly, you don’t have any other way to force the ReadLine() method to return. So catch the exception, check if CancellationPending is true and bail out when it is.

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

Sidebar

Related Questions

Today, I got completely surprised when I saw that a global variable has undefined
I've got a function that wants to access a global variable, the name of
I've got 2dim array set as global variable populated with numbers on row -
So I got a lot of responses from this site that global variables are
Say I've got a global variable which I'm accessing inside a PHP block, which
I have been storing a global variable that's needed across Activities in my Android
Simply, how do I create a global variable in JSP, such that I can
I've got a global command like this !i::Send {Up} (pressing alt+I will move the
I have got a problem with calling a global function, which takes a pointer
Got a seg fault from my memcpy that gdb can't give me anything else

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.