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

  • Home
  • SEARCH
  • 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 7759493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:38:38+00:00 2026-06-01T13:38:38+00:00

I have to make a Instant Messenger server in C#. The server is a

  • 0

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.

The question is how can I invoke a function from a separate thread, or make an event on a separate thread?

This is the main server class.

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);
     }
}

The NetServer class creates 2 Threads: acceptClientsThread and receiveMessagesThread.
The receive thread calls the NewNetMsg.

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!
               }
          }
     }
}

How can I make the msgHandler.HandleMsg function run in a separate thread or in the MainThread?

Like this, HandleMsg runs in the receiveMessagesThread.

The code above is pseudocode-ish. If there is anything ambigous please let me know.

  • 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-06-01T13:38:39+00:00Added an answer on June 1, 2026 at 1:38 pm

    There are a lot of different ways to move the HandleMsg call onto a different thread, depending on what your requirements are. The simplest way would be to raise the NewNetMsg event on a different thread using the ThreadPool:

    ThreadPool.QueueUserWorkItem(s => NewNetMsg(netClient, nextMsg));
    

    You could also use the Task Parallel Library (TPL). You could also add the event to a ConcurrentQueue that is processed by a dedicated background thread. And there are more options. Without more details it is impossible to give a more specific recommendation.

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

Sidebar

Related Questions

I have an Asp.net website using SQL Server. I want to make a website
I want to make user group system that imitates group policy in instant messengers.
Using this sample : I have make my own Fragment that holds tabhost and
I have to make a schema for an XML format that's already being used.
I have to make a decision of which database server to use for my
I am looking to build an instant messenger in Java. Clients will connect to
I have a datagrid that I want the user to sort the rows on.
What I want is for my app to have that zoom-in animation used by,
I will try and explain exactly what I want to achieve first. Imagine two
I have a Google Instant style jQuery search script that queries a PHP file

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.