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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:12:25+00:00 2026-05-25T01:12:25+00:00

I have a Server class which it basically waits for connections from a client.

  • 0

I have a Server class which it basically waits for connections from a client. Inside that class I create an NetworkStream object in order to be able to receive bytes from a client. Because the NetworkStream.Read() method is not asynchronous (meaning that it will wait until it reads bytes from a client in order to proceed executing code similar to the messagebox method), I have to read for bytes in a separate thread so that the user using the program can still interact with the program if the program happens to be waiting to read for data.

anyways a lot of objects are owned by that thread. One example is that I have a List called log in that class. I use that list to know the status of the server. Maybe it is listening for a connection or perhaps it’s status is “connected” or “disconnected”.

So if I do something like:

Server myServer = new Server("192.168.0.120","1300"...\\ I pass the appropite parameters in order to instantiate it

//...
.. then I am able to latter look at the log as
string foo = myServer.Log[0] for example.

because I want to know when the log is updated, on the server class I have created an event as:

    public delegate void onUpdateHandler(string newStatus);
    public event onUpdateHandler onUpdate = delegate { };

I then fire events on the Server class as:

 onUpdate("waitingForConnection");

and I receive those events with the method:

enter image description here

but if I try to do something with newStatus I get the error stating:

System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.

so how can I pass an object with an event?


Edit

so I also notice that if I do:

enter image description here

I also get an error!

but when I do the same thing calling that from a button as:

   // SERVER IS RUNNING BEFORE CALLING THIS METHOD
    private void button3_Click(object sender, RoutedEventArgs e)
    {

        listView1.Items.Add("my own string");

    }

I do NOT get an error!

why is it that I get an error with the event and I do not get an error when calling it with a regular button.

  • 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-25T01:12:25+00:00Added an answer on May 25, 2026 at 1:12 am

    The problem is that the thread tries to access the ListView which is a DependencyObject which has thread affinity, use the Dispatcher to execute methods like this on the UI-thread, e.g.:

    Application.Current.Dispatcher.Invoke((Action)(() =>
    {
        listView1.Items.Add(newStatus);
    }));
    

    Also see the threading model reference for additional info.

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

Sidebar

Related Questions

I have a standard windows server that inherits from the ServiceBase class. On the
I have written a custom server control which (pseudo-code) looks like public class MyCustomCtrl
I have a server that listens for a connection on a socket: public class
I`m writing client-server app for windows using WinSock and I have class for server.
I have a class that encapsulates tcp socket communications with a server. For each
I have a fairly simple class that I want to save to SQL Server
I have a server to which multiple clients can connect to. The client is
I wrote a small Server class which basically is a TcpListener wrapper and ThreadPool
I have to create a client/server system to stream video and audio. It would
I'd like to use the snoopy class, but I don't have the proper server

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.