title: How to block a thread until another thread gets a server response in C#
body: <p>I am working on a library where you can create a server with a name:</p>
  
  <pre><code>UtilityServer server = new UtilityServer("TestServer1", 3500);
  </code></pre>
  
  <p>You can also create a client with a name:</p>
  
  <pre><code>UtilityClient client = new UtilityClient("TestClient1");
  </code></pre>
  
  <p>The server creates a socket on the local computer with the port you choose. When the server is running, the client can connect via:</p>
  
  <pre><code>client.Connect(ServerIP, 3500);
  </code></pre>
  
  <p>Everything works fine when I do it event-based with for example:</p>
  
  <pre><code>client.UserNamesArrived += OnUserNamesArrived;
  client.RequestUserNames();
  
  private void OnUserNamesArrived(ReadOnlyCollection&lt;string&gt; userNames)
  {
      // Do something with userNames
  }
  </code></pre>
  
  <p>But when I try to create a blocking method in my library for example:</p>
  
  <pre><code>public ReadOnlyCollection&lt;string&gt; GetUserNames()
  {
      // Request userNames from server
      // Wait until server sent us userNames
      // return userNames
  }
  
  // Running on seperate thread, gets messages that server sents to client
  ReceiveMessages()
  {
      while(_isConnected)
      {
      // Waits until message is received (_strReader.ReadLine())
      // Looks what message contains (Can also be other things then userNames)
      // Gets collection of userNames when message contains it (Working)
      // Triggers event 'UserNamesArrived' with userNames as argument
      }
  }
  </code></pre>
  
  <p>I don't know how.</p>
  
  <p>Now here is my question, how do I wait in my <code>GetUserNames-Thread</code> until my <code>ReceiveMessages-Thread</code> gets the collection of <code>userNames</code> and how can I return them?</p>
  
  <p>There are similar Methods in the .Net-Framework that block the Thread, for example:</p>
  
  <pre><code>sqlCommand.ExecuteNonQuery();
  </code></pre>
  
  <p>This also blocks it and waits for a response, but how?</p>