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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:51:48+00:00 2026-06-05T00:51:48+00:00

I am writing a wpf application to control an embedded device over TCP. Writing

  • 0

I am writing a wpf application to control an embedded device over TCP. Writing to the device is easy, but I am having trouble receiving packets.

I have created a tcp NetworkStream and am using NetworkStream.BeginRead() to listen for TCP data. However, once I have composed a full packet, I would like to update my GUI to reflect the new information. It seems that I am not allowed to do this from the asynchronous callback thread.

It appears that there is a way to do this through the dispatcher one request at a time, but I need to update practically every control on my GUI. I am not writing a dispatcher function for every packet case and every WPF control. How do I get complete access to the WPF controls from my asynchronous thread?

EDIT:
Here is a code sample:

NetworkUnion union = (NetworkUnion)ar.AsyncState;
                union.BytesRead += tcpStream.EndRead(ar);

                if (union.BytesRead < union.TargetSize)
                    tcpStream.BeginRead(union.ByteArray, union.BytesRead, union.TargetSize - union.BytesRead, new AsyncCallback(ReadCommandCallback), union);
                else
                {
                    NetworkUnion payload = new NetworkUnion();
                    NetworkPacket pkt = (NetworkPacket)union.getArrayAsStruct();

                    // Respond to the packet
                    // Read the payload
                    payload.ByteArray = new byte[pkt.payloadSize];
                    tcpStream.Read(payload.ByteArray, 0, pkt.payloadSize);

                    // Determine what the payload is!
                    switch (pkt.code)
                    {
                        case (int)PacketCode.STATE:
                            payload.ObjectType = typeof(NetworkState);
                            NetworkState state = (NetworkState)payload.getArrayAsStruct();
                            Handle.fuelGauge.Value = Convert.ToDouble(state.mainFuel);
                            break;

When I try to update the fuel gauge, I get an InvalidOperationException, calling thread cannot access because another thread owns the object

The ‘Handle’ variable is used because this is from within a static utility class

The main question is, do I really have to replace every line of

Handle.fuelGauge.Value = Convert.ToDouble(state.mainFuel);

with

Handle.fuelGauge.Dispatcher.Invoke(
                                System.Windows.Threading.DispatcherPriority.Normal,
                                new Action(
                                    delegate()
                                    {
                                        Handle.fuelGauge.Value = Convert.ToDouble(state.mainFuel);
                                    }
                            ));

? It seems excessively repetitive. It would be easier to do something like:

Handle.mainWindow.Dispather.Lock();
Handle.fuelGauge.Value = Convert.ToDouble(state.mainFuel);
change everything else...
Handle.mainWindow.Dispatcher.Unlock();
  • 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-05T00:51:50+00:00Added an answer on June 5, 2026 at 12:51 am

    Generally, in .NET, you need to marshal UI requests back to the main UI thread. This is a common scenario when trying to update UI after a network read, because when using Sockets, you are usually always receiving data on some other (threadpool) thread.

    So, in your code which receives the data, you should wrap up the bytes into some useful object, and marshal that back to an entry point method on the main UI thread which can then take responsibility for farming out UI updates, on the now correct thread.

    There are many ways to do this, but perhaps a more flexible approach than some, would be for your callers of the socket code to pass in a SynchronizationContext object, which can then be used to marshal the received data to.

    See here for more info on SynchronizationContext

    You won’t need to post back every socket call, just move your swithc statement, that decides how to handle a packet, into a class on the UI thread, and invoke to that method. Then, that method can do UI updates as normal, as all calls from it are already marshalled correctly.

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

Sidebar

Related Questions

I am writing a WPF application and in one user control I am using
Im writing a WPF application that has a zoom and pan ability, but what
I am writing a WPF application on WinXP and I have overridden the default
I have some unit tests that I'm writing for a WPF application, and as
I'm writing a WPF User Control for my application, wrapping a ListBox and a
I'm writing a WPF desktop application and have a view that needs to look
I'm writing WPF application, that's using MVVMLight. I have a DataGrid and I wanna
I am writing my first WPF application. I have also installed Entity Framework (EF)
I have a richtextbox control in my WPF 4.0 application. Now suppose I have
In a WPF application I'm writing, I have a TransformedBitmap property which is bound

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.