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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:19:23+00:00 2026-05-19T00:19:23+00:00

This is my 1st C# project so I may be doing something obviously improper

  • 0

This is my 1st C# project so I may be doing something obviously improper in the code below.
I am using .NET, WinForms (I think), and this is a desktop application until I get the bugs out.
UpdateGui() uses Invoke((MethodInvoker)delegate to update various GUI controls based on received serial data and
sends a GetStatus() command out the serial port 4 times a second.
Thread Read() reads the response from serial port whenever it arrives which should be near immediate.
SerialPortFixer is a SerialPort IOException Workaround in C# I found at
http://zachsaw.blogspot.com/2010/07/serialport-ioexception-workaround-in-c.html.

After one or both threads die I’ll see something like
The thread 0x1288 has exited with code 0 (0x0).
in the debug code output.

Why do UpdateGui() and/or Read() eventually die?

public partial class UpdateStatus : Form
{
    private readonly byte[] Command = new byte[32];
    private readonly byte[] Status = new byte[32];
    readonly Thread readThread;
    private static readonly Mutex commandMutex = new Mutex();
    private static readonly Mutex statusMutex = new Mutex();
    ...

    public UpdateStatus()
    {
        InitializeComponent();
        SerialPortFixer.Execute("COM2");
        if (serialPort1.IsOpen)
        {
            serialPort1.Close();
        }
        try
        {
            serialPort1.Open();
        }
        catch (Exception e)
        {
            labelWarning.Text = LOST_COMMUNICATIONS + e;
            labelStatus.Text = LOST_COMMUNICATIONS + e;
            labelWarning.Visible = true;
        }
        readThread = new Thread(Read);
        readThread.Start();
        new Timer(UpdateGui, null, 0, 250);
    }
    static void ProcessStatus(byte[] status)
    {
        Status.State = (State) status[4];
        Status.Speed = status[6]; // MSB
        Status.Speed *= 256;
        Status.Speed += status[5];
        var Speed = Status.Speed/GEAR_RATIO;
        Status.Speed = (int) Speed;
        ...
    }
    public void Read()
    {
        while (serialPort1 != null)
        {
            try
            {
                serialPort1.Read(Status, 0, 1);
                if (Status[0] != StartCharacter[0]) continue;
                serialPort1.Read(Status, 1, 1);
                if (Status[1] != StartCharacter[1]) continue;
                serialPort1.Read(Status, 2, 1);
                if (Status[2] != (int)Command.GetStatus) continue;
                serialPort1.Read(Status, 3, 1);
                ...
                statusMutex.WaitOne();
                ProcessStatus(Status);
                Status.update = true;
                statusMutex.ReleaseMutex();
            }
            catch (Exception e)
            {
                Console.WriteLine(@"ERROR! Read() " + e);
            }
        }
    }
    public void GetStatus()
    {
        const int parameterLength = 0; // For GetStatus
        statusMutex.WaitOne();
        Status.update = false;
        statusMutex.ReleaseMutex();
        commandMutex.WaitOne();
        if (!SendCommand(Command.GetStatus, parameterLength))
        {
            Console.WriteLine(@"ERROR! SendCommand(GetStatus)");
        }
        commandMutex.ReleaseMutex();
    }
    private void UpdateGui(object x)
    {
        try
        {                
            Invoke((MethodInvoker)delegate
            {
                Text = DateTime.Now.ToLongTimeString();
                statusMutex.WaitOne();
                if (Status.update)
                {
                    if (Status.Speed > progressBarSpeed.Maximum)
                    {
                        Status.Speed = progressBarSpeed.Maximum;
                    }
                    progressBarSpeed.Value = Status.Speed;
                    labelSpeed.Text = Status.Speed + RPM;
                    ...
                }
                else
                {
                    labelWarning.Text = LOST_COMMUNICATIONS;
                    labelStatus.Text = LOST_COMMUNICATIONS;
                    labelWarning.Visible = true;
                }
                statusMutex.ReleaseMutex();
                GetStatus();
            });
        }
        catch (Exception e)
        {
            Console.WriteLine(@"ERROR! UpdateGui() " + e);
        }
    }
}
  • 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-05-19T00:19:24+00:00Added an answer on May 19, 2026 at 12:19 am

    A thread will terminate when there’s no more code to execute, or more specifically when the method you specify when you create thread returns.

    Maybe serialport1 becomes null?

    As for the update timer, there is a special purpose windows forms timer that runs periodically that doesn’t require you to use Invoke. It’s the right tool for the job

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

Sidebar

Related Questions

I'm working on my 1st project using MS Unity IoC framework. If I have
I am trying to create my 1st project in django using jython. I am
This past summer I was developing a basic ASP.NET/SQL Server CRUD app, and unit
I've created a simple solution with 2 projects. The 1st project (class library) contains
I'm in the process of building a site with CodeIgniter. This is the 1st
1st part Never worked on team who uses different IDE for one project.. (But
My Django project's directory hierarchy looks like this: + pybsd |---+ devices |---+ templates
In my project, my /PropertDetail.aspx can get 2 querystrings. 1st one for the PropertyId
This is my code: OleDbConnection connection = new OleDbConnection( Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\Offline.accdb;Persist Security Info=False); connection.Open();
This is my first time using Magento. I upgraded this site from 1.4.1.1 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.