1

I came to know that Thread.Suspend is not a good way to pause a thread indefinitely. Please let me know if other way to achieve same.

Thanks in advance.

chaituse
  • 21
  • 1
  • 8
  • See this 'System.Threading.EventWaitHandle' http://stackoverflow.com/questions/4848064/c-sharp-how-to-pause-the-thread-and-continue-when-some-event-occur – user1477388 Jul 31 '13 at 14:15
  • 1
    There's no point in suspending a thread forever. Why not just `Abort`-ing it? At least then it won't use up system resources anymore. (Or use the `ThreadPool` for short work items, and you won't have to think about suspending or cleaning up worker threads.) – stakx - no longer contributing Jul 31 '13 at 14:15
  • There is some really good explaination at http://stackoverflow.com/questions/142826/is-there-a-way-to-indefinitely-pause-a-thread – Prash Jul 31 '13 at 14:17
  • I am trying to develop a windows form application. Whenever I press pause button, it has to pause until I press resume button. This is what my requirement is. – chaituse Jul 31 '13 at 14:19
  • The link that I shared (in the comment above) have a sample program as well. – Prash Jul 31 '13 at 14:22
  • @Prash Thank you for your help. Actually, I was looking for Vb.Net code in particular. I dont know C#. Its really difficult for me to interpret. – chaituse Jul 31 '13 at 14:30
  • Understood. Just FYI. Framework being the same irrespective of language (whether C# or VB.NET), it would never be a big concern. Also there are many online converters available online; most famous thing being developer fusion - http://www.developerfusion.com/tools/convert/csharp-to-vb/ – Prash Jul 31 '13 at 14:33

3 Answers3

1

A short vb example

Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    thrd.IsBackground = True
    thrd.Start()
End Sub

Dim thrd As New Threading.Thread(AddressOf somethread)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'pause and resume
    'first click pauses, second click resumes
    reqpause.Set() 'set event
End Sub

Dim reqpause As New Threading.AutoResetEvent(False)

Private Sub somethread()
    Do
        'simulate work - your code here
        Threading.Thread.Sleep(500)
        Debug.WriteLine(DateTime.Now.ToLongTimeString)
        'end simulate work

        If reqpause.WaitOne(0) Then 'pause requested?
            Debug.WriteLine("t-thrd paused")
            reqpause.WaitOne() 'wait here for continuation
            Debug.WriteLine("t-continue thrd")
        End If
    Loop
End Sub
dbasnett
  • 11,334
  • 2
  • 25
  • 33
0

Using Suspend and Resume is not a good idea. I am not sure why you want to wait forever, but to wait use an EventWaitHandle to wait.

private EventWaitHandle handle = new AutoResetEvent();

private void WorkerThread() 
{
    while(true) 
    {
        handle.WaitOne();
    }
}
//Forever is a long time 
public void StopWaitingForever()
{
    handle.Set();
}
Jon Raynor
  • 3,804
  • 6
  • 29
  • 43
0

Other simpler way is to call Sleep(Timeout.Infinite).

Reference here.

Zac
  • 4,510
  • 3
  • 36
  • 44