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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:03:29+00:00 2026-06-01T08:03:29+00:00

I have a thread in a function which start the live monitoring, it basically,

  • 0

I have a thread in a function which start the live monitoring, it basically, opens the serial port and continously reading data from serial port. However, If I need to terminate this thread, how should I do it? Because If I don’t terminate the running thread which opened the specific serial port and reading data. When I close it and call the function again. The same serial port cannot opened. I suspect that the serial port is not closed properly and is still running in a seperate thread. So I think i have to terminate that thread in order to open the same serial port again next time. Does anyone have any idea of how to achieve this?

I have seen some forum said that Thread.Abort() is dangerous to use. It should only be used in last resort.

Thanks for any helps.

Charles

  • 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-01T08:03:30+00:00Added an answer on June 1, 2026 at 8:03 am

    Generally, you design the method that is run in the background thread to listen for cancellation requests. This can be as simple as a boolean value:

    //this simply provides a synchronized reference wrapper for the Boolean,
    //and prevents trying to "un-cancel"
    public class ThreadStatus
    {
       private bool cancelled;
    
       private object syncObj = new Object();
    
       public void Cancel() {lock(syncObj){cancelled = true;}}
    
       public bool IsCancelPending{get{lock(syncObj){return cancelled;}}}
    }
    
    public void RunListener(object status)
    {
       var threadStatus = (ThreadStatus)status;
    
       var listener = new SerialPort("COM1");
    
       listener.Open();
    
       //this will loop until we cancel it, the port closes, 
       //or DoSomethingWithData indicates we should get out
       while(!status.IsCancelPending 
             && listener.IsOpen 
             && DoSomethingWithData(listener.ReadExisting())
          Thread.Yield(); //avoid burning the CPU when there isn't anything for this thread
    
       listener.Dispose();
    }
    
    ...
    
    Thread backgroundThread = new Thread(RunListener);
    ThreadStatus status = new ThreadStatus();
    backgroundThread.Start(status);
    
    ...
    
    //when you need to get out...
    //signal the thread to stop looping
    status.Cancel();
    //and block this thread until the background thread ends normally.
    backgroundThread.Join()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function call from a thread: Thread Move = new Thread(){
I have a thread, which does a function call, Thread 1() { while(1) {
I have a main thread that invokes a child thread function at different times
I have a thread that, when its function exits its loop (the exit is
This question is based on the thread . I have the shell function function
I have a function that is being called from different threads in the application.
I have thread exception handler which saves the exception stack trace and should close
I'm want to check for incoming data packages on the serial port, using boost.asio
I have the following function, which returns a filesize of a file over HTTP:
I have a function which needs to be invoked with a different number of

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.