0

Next piece of code throws a ThreadStateException:

public void StartListening()
{
     this.isListening = true;
     if (!this.listeningThread.IsAlive)
         this.listeningThread = new Thread(ListenForClients);
     this.listeningThread.Start();
     this.listeningThread.IsBackground = true;
}

And while setting the IsBackground property

this.listeningThread.IsBackground = true;

the exception is thrown.

What's wrong? Am I using IsBackground=true in the wrong place?

Exception text:

Thread is dead; state cannot be accessed.
at System.Threading.Thread.SetBackgroundNative(Boolean isBackground)
at System.Threading.Thread.set_IsBackgrounf(Boolean value)
at MyNamespace.MyClass.StartListening()
...

IsBackground property set up only in one place, here. So, it never changes during thread work. Unfortunately I can't reproduce this (reproduced on client's system only), so I don't know the reason. And that's why I'm asking.

Max
  • 12,622
  • 16
  • 73
  • 101
Ksice
  • 3,277
  • 9
  • 43
  • 67
  • 3
    I'm not sure that you can change whether a thread is background or foreground after starting the thread.. may be wrong – Sayse May 20 '14 at 06:56
  • 2
    The exception message clearly indicates that the `Thread is dead`. Perhaps it has already finished? Regardless, I would set any properties on the `Thread` *before* starting it. – Jonathon Reinhart May 20 '14 at 06:58
  • Did you try to set the propertie `this.listeningThread.IsBackground = true;`, before you start the thread? – C0d1ngJammer May 20 '14 at 06:58
  • 2
    A thread can be changed to a background thread at **any time** by setting its IsBackground property. http://msdn.microsoft.com/en-us/library/h339syd0(v=vs.110).aspx – ClotzA May 20 '14 at 06:58
  • 2
    @Sayse The [documentation](http://msdn.microsoft.com/en-us/library/system.threading.thread.isbackground.aspx) does indicate any restriction on when you can set this property, except that the thread cannot be dead. – Jonathon Reinhart May 20 '14 at 06:59
  • @JonathonReinhart - It must be a very edge case thing to need to change it between back/foreground during operation. Thank you for the link too ClotzA (for clarification) – Sayse May 20 '14 at 07:01
  • could you please explain more about your task? why you call `listeningThread.Start()` in any way? thanks – alex.b May 20 '14 at 07:10

1 Answers1

6

The most reason why you're getting the error is because at the moment you set this.listeningThread.IsBackground = true the thread is already dead.

Let me explain:

 this.isListening = true;
 if (!this.listeningThread.IsAlive)// thread is alive
     this.listeningThread = new Thread(ListenForClients);
 this.listeningThread.Start();// thread is alive, still ..
 // thread completes here
 // you might add some delay here to reproduce error more often
 this.listeningThread.IsBackground = true;

I don't know the full context of the task, but I think it makes sense to change code to:

public void StartListening()
{
 this.isListening = true;
 if (!this.listeningThread.IsAlive)
 {
     this.listeningThread = new Thread(ListenForClients);
     this.listeningThread.IsBackground = true;
     this.listeningThread.Start();
 }
 // else { do nothing as it's already alive }
}
alex.b
  • 4,547
  • 1
  • 31
  • 52