0

Here is how I resume a suspended thread.

  if SerialThread.ThreadState = ThreadState.Suspended then
    SerialThread.Resume;

Although the above code doesn't raise compiler error or syntax error, it does however raise warning as follows;

TSerialIndicator.pas(77,18): warning PW3: Obsolete: "System.Threading.Thread.Resume has been deprecated. Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources.

So, what is the proper way in .NET to resume a suspended thread?

Ken White
  • 123,280
  • 14
  • 225
  • 444
ThN
  • 3,235
  • 3
  • 57
  • 115
  • 1
    `Thread.Suspend()` is also deprecated. – SLaks Feb 08 '13 at 16:08
  • @SLaks, Yes, you are right. Does this mean one shouldn't use them for they may cause problem in your program? Except for the warning, the program seem to be okay with it. However, I am running into problems. – ThN Feb 08 '13 at 16:10
  • 2
    Suspending and resuming threads are inherently dangerous and unsafe operations. You shouldn't do either. – SLaks Feb 08 '13 at 16:12
  • @Slaks okay...So what do you suggest? Thanks. – ThN Feb 08 '13 at 16:13
  • What do I suggest for what? – SLaks Feb 08 '13 at 16:13
  • @Slaks, I thought you would have suggestion as to how I should be doing this. If they are unsafe, how do you do that then without using them. – ThN Feb 08 '13 at 16:16
  • @digitalanalog post a *new* question describing *what you're trying to achieve* by suspending/resuming threads. You'll probably receive several good advices on the alternatives. – Anton Kovalenko Feb 08 '13 at 16:18
  • @AntonKovalenko, I thought the question was clear. I am asking for an alternative if compiler is warning that Suspend and Resume methods are obsolete or deprecated. – ThN Feb 08 '13 at 16:21
  • @digitalanalog the **thing** you're trying to do is deprecated, not just methods. You can go ahead and ignore the warnings, or you can *solve your real problem* (...if only we knew it) in some other way. – Anton Kovalenko Feb 08 '13 at 16:22
  • 2
    @digitalanalog maybe this can help http://stackoverflow.com/questions/382173/what-are-alternative-ways-to-suspend-and-resume-a-thread – Zaki Feb 08 '13 at 16:24
  • 3
    @digitalanalog: The entire _concept_ of suspending and resuming a thread is inherently dangerous. You need to do something completely different. What problem are you trying to solve? – SLaks Feb 08 '13 at 16:38

1 Answers1

0

AutoResetEvent is great for "suspending" a thread, your thread code just calls event.Wait till it has something to, when the caller calls event.Set() which breaks the wait.

Carlo Kok
  • 1,128
  • 5
  • 14