How to stop threads?

前端 未结 6 1032
清酒与你
清酒与你 2021-01-23 07:45

Hi guys I start threads with such code:

    Thread[] thr;
    private void button1_Click(object sender, EventArgs e)
    {
        decimal value = numericUpDown2         


        
6条回答
  •  心在旅途
    2021-01-23 08:22

    You can use a CancellationToken to signal when the operation should stop.

    1. Create a CancellationTokenSource as an instance field of your type that you initialize in the button click handler.

    2. In your background method periodically check the IsCancellationRequested property of the Token in the token source, or call ThrowIfCancellationRequested() if you want it to just throw an exception if it is canceled.

    3. When you want to stop the threads call Cancel on the token source.

提交回复
热议问题