title: create object in thread and pass sockets WPF c#
body: <p>i need to create an object in thread and pass to him sockets to send message but when i create the object the connected status in the object is false. and i cant send message
  someone can help me?</p>
  <p>this is my code:</p>
  <pre><code> this.Dispatcher.BeginInvoke(new Action(() =&gt;
   {
         game = new Game(clientStream);
         game.setGame(message);
         game.Client = clientStream;
         this.Close();
         game.ShowDialog();
   }));
  </code></pre>
  <p>this how i create the thread in the constractur</p>
  <pre><code>    ThreadStart checkForActions = new ThreadStart(checkForCloseOrStart);
      actions = new Thread(checkForActions);
      actions.Start();
  </code></pre>
  <p>and this is the whole thread function</p>
  <pre><code>    bool keepListening = true;
  
      while (keepListening)
      {
          try
          {
              string code = &quot;&quot;;
              byte[] buffer = new byte[4096];
              int bytesRead = clientStream.Read(buffer, 0, 4096);
              string msg = System.Text.Encoding.Default.GetString(buffer);
              for (int i = 0; i &lt; bytesRead; i++)
              {
                  code += msg[i];
              }
              string[] message = code.Split('#');
              if (message[0] == &quot;108&quot;)
              {
                  int j = 0;
                  string[] users = new string[message.Length - 2];
                  for (int i = 2; i &lt; message.Length; i++)
                  {
                      users[j] = message[i];
                      j++;
                  }
                  this.Dispatcher.BeginInvoke(new Action(() =&gt;
                  {
                      usersList.ItemsSource = users;
                  }));
              }
              else if (message[0] == &quot;116&quot;)
              {
                  keepListening = false;
                  this.Dispatcher.BeginInvoke(new Action(() =&gt;
                  {
                      this.Close();
                  }));
              }
              else if (message[0] == &quot;118&quot;)
              {
                  keepListening = false;
                  this.Dispatcher.BeginInvoke(new Action(() =&gt;
                  {
                      game = new Game(clientStream);
                      game.setGame(message);
                      game.Client = clientStream;
                      this.Close();
                      game.ShowDialog();
                  }));
              }
          }
          catch
          { }
      }
  </code></pre>