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 6556917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:00:26+00:00 2026-05-25T13:00:26+00:00

I am creating a C# server for a game that I am creating and

  • 0

I am creating a C# server for a game that I am creating and am new to multithreading and socket programming. My server should be able to hold multiple clients all in seperate threads. My server is run as a console application. My only problem is that when one of my test clients exits the application I don’t know how to stop the thread that they were running. How do you stop a thread while its running?

  • 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-05-25T13:00:26+00:00Added an answer on May 25, 2026 at 1:00 pm

    I’m going to assume that your threads are essentially message-processing threads that wait for a request from the client and then service a response. If that is the case, your thread’s main loop might look something like the HandleClientRequests method below. Note that this example assumes a graceful disconnect. A robust implementation would also account for the possibility that a client disconnects without executing the proper shutdown sequence or that the connection fails at some point.

    public class ClientConnection {
        private Socket _socket;        
    
        public ClientConnection(Socket socket) {
            _socket = socket;
            new Thread(HandleClientRequests);
        }
    
        /* snip */
    
        public void HandleClientRequests() {
            bool disconnectRequested = false;
            while(!disconnectRequested) {
                var message = ReadNextMessageFromSocket();
                if(message.Type == MessageType.CloseConnection) {
                    disconnectRequested = true;
                } else {
                    //service request and send response
                }
            }
            ShutdownConnection();
            //after this point the thread terminates
        }
    }
    

    Whatever the case, you’re ThreadStart method should usually have some kind of loop, within which you do whatever it is you want your thread to do, and that loop should have a condition that is used to signal that it’s time for the thread to terminate.

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

Sidebar

Related Questions

I am creating a program where multiple clients can connect to a server. The
Creating a server-side socket will fail if I'm trying to use the same port
I'm working on refactoring a suite of old utilities and creating a new server
When creating a new build in Team Foundation Server, I get the following error
I am creating a custom ASP.NET AJAX server control in which multiple instances of
I am creating a little game client that will end up connecting to a
We're creating a game that will run on iOS, Android and in the web
I am creating a program which launches a game server. I have combobox in
Im creating flash game that have the functionality to capture/record its gameplay that can
I'm creating a simple online multiplayer game, with which two players (clients) can play

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.