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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:54:15+00:00 2026-06-02T16:54:15+00:00

I am writing a multithreaded application where a thread locks the serialport and continuously

  • 0

I am writing a multithreaded application where a thread locks the serialport and continuously sends commands and gets responses using SerialPort.Write(command) and SerialPort.ReadLine()
in a loop untill it gets an “OK” response for one particular command.

Is it possible for the thread to switch during a ReadLine() call and cause a TimeOutException.

I have a timeout set at 2000ms.

I have random TmeOutExceptions after the program runs for a few minutes (about 5 – 10)

23332   10:14:23 AM AutoLoader.WinS IRP_MJ_WRITE    Serial0 SUCCESS Length 9: STG|4.5.. 
23336   10:14:23 AM AutoLoader.WinS IRP_MJ_READ Serial0 SUCCESS Length 1: O 
23339   10:15:54 AM AutoLoader.WinS IRP_MJ_WRITE    Serial0 SUCCESS Length 7: GTSNS..   
23342   10:15:54 AM AutoLoader.WinS IRP_MJ_READ Serial0 SUCCESS Length 3: K..   
23347   10:15:54 AM AutoLoader.WinS IRP_MJ_WRITE    Serial0 SUCCESS Length 7: GLSTS..   
23351   10:15:54 AM AutoLoader.WinS IRP_MJ_READ Serial0 SUCCESS Length 1: O 
23355   10:15:54 AM AutoLoader.WinS IRP_MJ_READ Serial0 SUCCESS Length 3: K..

After 10:14:23 AM I get a TimeOutException which I suppressed.

                    while (response != "OK")
                    {
                        Thread.Sleep(1000);

                        _alComm.SendString("SSD|" + CurrentData.MeasuredDepthReturns + Environment.NewLine);
                        response = _alComm.ReceiveString(2000);

                        _alComm.SendString("STG|" + CurrentData.AvgGas + Environment.NewLine);
                        response = _alComm.ReceiveString(2000);

                        _alComm.SendString("GTSNS\r\n");
                        response = _alComm.ReceiveString(2000);
                        SetSamplingTimePercent(response);
                    }

and

public void SendString(string sDataToSend)
{
    if (sDataToSend == "")
        return;

    try
    {
        AlPort.Write(sDataToSend);
    }
    catch (Exception ex)
    {
        //MessageBox.Show(ex.ToString(), "Error");
    }
}


public string ReceiveString(int timeOut)
{
    string inString = null;
    try
    {
        // set read timeout
        AlPort.ReadTimeout = timeOut;
        inString = AlPort.ReadLine();
    }
    catch (TimeoutException ex)
    {
        return string.Empty;
    }

    return inString;
}
  • 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-02T16:54:18+00:00Added an answer on June 2, 2026 at 4:54 pm

    A ReadLine (or any I/O for that matter) will cause a context-switch until it is serviced. Now, the way I see there are two possibilities:

    1. Either the thread stays suspended by the OS until the timeout elapses and the exception gets thrown or
    2. You stay suspended until the I/O is serviced at which point with high probability the OS will wake your thread immediately and preempt other threads to allow your code to run. Remember that any modern OS gives a generous priority boost to I/O bound threads.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a multithreaded socket application in Python using the socket module. the
I'm writing a multithreaded Java program where each thread potentially needs its standard output
I'm writing a multi-threaded server using boost::asio (for sockets), boost::thread (for threading), libconfig++ (for
I'm writing a highly parallel application that's multithreaded. I've already got an SSE accelerated
I'm writing a multi-threaded application, where there is a main thread which distributes tasks
I've got a multithreaded OpenGL application using PBOs for data transfers between cpu and
I'm building a multithreaded application with pthreads and need a thread to periodically check
I am writing a multithreaded application and would like to pass around pointers to
I am writing a multi-threaded c++ application. When thread A has a very computationally
I'm fairly new to C but writing a small multithreaded application. I want to

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.