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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:47:02+00:00 2026-05-30T09:47:02+00:00

I have a class called SerialClient that sends/receives bytes over a serial port. When

  • 0

I have a class called SerialClient that sends/receives bytes over a serial port. When SerialClient.Start() is called it goes into an infinite while loop, until SerialClient.Stop() is called. The class has some properties, for simplicity lets say SerialClient.PropA, SerialClient.PropB.

I need to use this in a GUI. Where by on the GUI you can call Start, Stop and read the properties as they change.

In order to use this in a GUI, obviously SerialClient needs to operate in a thread as its an endless loop. So, I have setup the view with properties to match that of SerialClient. The view calls Start() and Stop() on the presenter.

In the presenter I create a new thread and call start on an object of SerialClient. The problem is when _serialClient throws an exception the presenter cannot catch it, and also if the properties like (SerialClient.PropB) change and call their changed event handlers, they need to be invoked on the GUI thread. I tried using the background worker, but couldn’t get it working (changing properties still hit the UI from the non-UI thread).

Thread thread = new Thread(_serialClient.Start);
thread.IsBackground = true;
thread.Start();

There must a simple solution I am over looking. The GUI just needs to call methods and read properties, and prevent exceptions crashing the whole application.

Any ideas?

  • 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-30T09:47:03+00:00Added an answer on May 30, 2026 at 9:47 am

    You hit 2 of the common problems usually seen when dealing with multithreaded application in C#:

    1. Dealing with exception in the background thread. When exceptions occurs in the background thread, they travel up the stack, from caller to caller to see if anyone is able to catch that exception. If the original call occurs from outside your own calls (for example, a callback from a serial port or a timer) you may or may not be notified of the exception. As a typical solution to this, what I have seen is to wrap in a try-catch the code that can throw the exception and to have an event handler push the exception to a thread that will be able to handle the exception appropriately (log, notify the user, terminate, all of the above)

    2. Events are always excuted on the thread that calls them. You have to manually marshall the call to the UI thread using Dispatcher and Invoke. The most succint code to do it I found is here

     
     void someEvent_Handler(object sender, SomeEventEventArgs e)
    {
        if (this.Dispatcher.CheckAccess())
        {
            // do work on UI thread
        }
        else
        {
            // or BeginInvoke()
            this.Dispatcher.Invoke(new Action(someEvent_Handler), 
                sender, e);
        }
    }
     
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called DatabaseHelper that wraps a DbConnection. What's the proper way
I have a class called UserInfo that contains details about a given user. There
I have a class called Question that has a property called Type. Based on
I have a class called Image and a class that extends it called ImageSheet
I have a class called GraphView that extends UIView that basically draws a small
I have a container class (called Atom) that I want to store objects of
I have a class called Foo that has a function that looks like the
I have a class called Partner, that I need to Submit all the info
I have a class called Foo that defines a list of objects of type
I have a class called Profile that has some simple properties and then it

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.