title: access object from a different thread
body: <p>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. </p>
  
  <p>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".</p>
  
  <p>So if I do something like:</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>because I want to know when the log is updated, on the server class I have created an event as:</p>
  
  <pre><code>    public delegate void onUpdateHandler(string newStatus);
      public event onUpdateHandler onUpdate = delegate { };
  </code></pre>
  
  <p>I then fire events on the Server class as:</p>
  
  <pre><code> onUpdate("waitingForConnection");
  </code></pre>
  
  <p>and I receive those events with the method:</p>
  
  <p><img src="https://i.stack.imgur.com/ck8Z7.png" alt="enter image description here"></p>
  
  <p>but if I try to do something with newStatus I get the error stating:</p>
  
  <pre><code>System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
  </code></pre>
  
  <p>so how can I pass an object with an event? </p>
  
  <hr>
  
  <p>Edit </p>
  
  <p>so I also notice that if I do:</p>
  
  <p><img src="https://i.stack.imgur.com/VNH3N.png" alt="enter image description here"></p>
  
  <p>I also get an error!</p>
  
  <p>but when I do the same thing calling that from a button as:</p>
  
  <pre><code>   // SERVER IS RUNNING BEFORE CALLING THIS METHOD
      private void button3_Click(object sender, RoutedEventArgs e)
      {
  
          listView1.Items.Add("my own string");
  
      }
  </code></pre>
  
  <p>I do NOT get an error!</p>
  
  <p>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.</p>