C#C
C#2y ago
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
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
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();
        
    }
}
Was this page helpful?