© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
17 replies
ChezZ

Creating a thread that lives for as long as the application.

I wrote a server using a c library, I created two threads using
new Thread
new Thread
and have an infinite loop running in the threads method. One thread handles all socket related code, and adds incoming packets to be deserialized to a single producer single consumer queue. The other thread deserializes the packets. The codes works but it has a flaw. Creating the thread via
new Thread
new Thread
fails and I get an exception if the system running the app has no more threads available. is it possible to tell the app use an existing thread if creating a new one fails:

thread = new Thread(DoWork); //this line of code throws the exception if the cpu runs out of threads
thread.IsBackground = true;
shouldRun = true;

private void DoWork()
{
    
    Init();
    while (shouldRun == true)
    {
        Consume();
        Tick();
        
    }
}
thread = new Thread(DoWork); //this line of code throws the exception if the cpu runs out of threads
thread.IsBackground = true;
shouldRun = true;

private void DoWork()
{
    
    Init();
    while (shouldRun == true)
    {
        Consume();
        Tick();
        
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ creating a CLI application
C#CC# / help
2y ago
Application called an interface that was marshalled for a different thread?
C#CC# / help
4y ago
✅ Creating an installer for application
C#CC# / help
2y ago
creating am installer for windows application
C#CC# / help
2y ago