title: Invoke/Call an even in a different thread
body: <p>I have to make a Instant Messenger server in C#. The server is a ConsoleApplication project. And I want to make a server that runs in 3 threads. I will explain in the code below.</p>
  
  <p>The question is how can I invoke a function from a separate thread, or make an event on a separate thread?</p>
  
  <p>This is the main server class.</p>
  
  <pre><code>public class GiNetServer
  {
       public void Start()
       {
           netServer  = new NetServer();
           msgHandler = new NetMsgHandler();
  
           netServer.NewNetMsg += msgHandler.HandleMsg;
  
           Work();
       }
  
       private void Work()
       {
           while(true)  //This runs in the MainThread
             sleep(1);
       }
  }
  </code></pre>
  
  <p>The NetServer class creates 2 Threads: acceptClientsThread and receiveMessagesThread.
  The receive thread calls the NewNetMsg.</p>
  
  <pre><code>public class NetServer
  {
  
       public event NewNetMsgEventHandler NewNetMsg;
  
       public NetServer()
       {
            acceptClientsThread = new Thread(ListenForClients);
            receiveMessageThread = new Thread(Receive);
            //and of course starts them here...
       }
  
       private void Receive()
       {
            while(true)
            {
                 Heartbeat();
            }
       }
  
       private void Heartbeat()
       {
            foreach(netClient in clientsList)
            {
                 if (netClient.DataAvalible)
                 {
                     netClient.Recive();
                 }
                 if (!netClient.IsBufferEmpty())
                 {
                     nextMsg = netClient.NextMessage();
                     if (nextMsg != null)
                         NewNetMsg(netClient, nextMsg);  //Call the event!
                 }
            }
       }
  }
  </code></pre>
  
  <p>How can I make the msgHandler.HandleMsg function run in a separate thread or in the MainThread?</p>
  
  <p>Like this, HandleMsg runs in the receiveMessagesThread.</p>
  
  <p>The code above is pseudocode-ish. If there is anything ambigous please let me know.</p>