How to suspend a stream by its name from the main stream?

I have a local thread that starts when a button is clicked from the main thread. His name

"RegistrationThread". Now I want to stop this thread on another click event of the stop button. how

I stop "RegistrationThread", which is a local thread, and I cannot access this thread by its

name. If I get "RegistrationThread" then it can be paused. How to do it?

+3
source share
2 answers

. , . , . -. - , , .

public class YourForm : Form
{
  private volatile bool m_Stop = false;

  private void StartButton_Click(object sender, EventArgs e)
  {
    var thread = new Thread(
      () =>
      {
        while (...)
        {
          // Periodically poll the m_Stop flag.
          if (m_Stop)
          {
            break;
          }
        }
      });
    thread.Start();
  }

  private void StopButton_Click(object sender, EventArgs e)
  {
    m_Stop = true;
  }
}

, Thread, Abort .

+1

, , Thread.Name . ?

() .net, .

Thread.Abort. , , , . , MSDN , Abort() "" . , API , .

, - , . , .net (, ), . microsoft Thread.Suspend .

, , , , .

+1

Source: https://habr.com/ru/post/1761716/


All Articles