0

I have a problem with task and thread. In UI web, we use task to run function and this function is called from another class and this function in class also use Thread to run. Question: How to pause and resume task and threads through by button in UI?

frank
  • 23
  • 3

1 Answers1

0

Suspend thread in .net, you can by using the method Thread.Suspend, but this method is not recommended to use it, here discussed this problem.

You should consider implementing its own logic of suspension, for example by means of a class ManualEventReset. The code below.

    private static ManualResetEvent _pauseEvent = new ManualResetEvent(true);

    public static void YourTaskMethod()
    {
        for (;;)
        {
            //Do some thing
            _pauseEvent.WaitOne();
        }
    }

    public static void PauseButton()
    {
        _pauseEvent.Reset();
    }

    public static void PlayButton()
    {
        _pauseEvent.Set();
    }
Community
  • 1
  • 1
MIVer
  • 144
  • 5