C#多线程之线程控制详解

时间:2022-02-22 17:40:49

本文为大家分享了c#多线程之线程控制,供大家参考,具体内容如下

方案一:

调用线程控制方法.启动:thread.start();停止:thread.abort();暂停:thread.suspend();继续:thread.resume();

C#多线程之线程控制详解

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private void btn_start_click(object sender, eventargs e)
   {
     mthread.start(); // 开始
   }
 
   private void btn_stop_click(object sender, eventargs e)
   {
     mthread.abort(); // 终止
   }
 
   private void btn_suspend_click(object sender, eventargs e)
   {
     mthread.suspend(); // 暂停
   }
 
   private void btn_resume_click(object sender, eventargs e)
   {
     mthread.resume(); // 继续
   }

线程定义为:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
mthread = new thread(() =>
      {
        try
        {
          for (int j = 0; j < 20; j++)
          {
            int vsum = 0;
            this.textbox1.text += "--->";
            for (int i = 0; i < 100000000; i++)
            {
              if (i % 2 == 0)
              {
                vsum += i;
              }
              else
              {
                vsum -= i;
              }
            }
            this.textbox1.text += string.format("{0} => vsum = {1}\r\n", datetime.now.tostring(), vsum);
            thread.sleep(1000);
          }
        }
        catch (threadabortexception ex)
        {
          console.writeline("threadabortexception:{0}", ex.message);
        }
      });

值得注意的是: 通过 thread.abort() 停下来的线程(或自行运行结束的线程),都无法直接通过 thread.start() 方法再次启动,必须重新创建一个线程启动。

所以,“开始按钮”事件应为:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
private void btn_start_click(object sender, eventargs e)
    {
      // 定义线程
      mthread = new thread(() => // lambda 表达式
      {
        try
        {
          for (int j = 0; j < 20; j++)
          {
            int vsum = 0;
            this.textbox1.text += "--->";
            for (int i = 0; i < 100000000; i++)
            {
              if (i % 2 == 0)
              {
                vsum += i;
              }
              else
              {
                vsum -= i;
              }
            }
            this.textbox1.text += string.format("{0} => vsum = {1}\r\n", datetime.now.tostring(), vsum);
            thread.sleep(1000);
          }
        }
        catch (threadabortexception ex)
        {
          console.writeline("threadabortexception:{0}", ex.message);
        }
      });
 
      mthread.start(); // 开始
    }

此外,对于 thread.suspend() 和 thread.resume() 方法,微软已经将其标记为过时:

thread.suspend has been deprecated.  please use other classes in system.threading, such as monitor, mutex, event, and semaphore, to synchronize threads or protect resources.  (thread.suspend 已被否决。请使用系统中的其他类线程,如监视器、互斥体、事件和信号量,以同步线程或保护资源。)

因为,无法判断当前挂起线程时它正在执行什么代码。如果在安全权限评估期间挂起持有锁的线程,则 appdoamin 中的其它线程可能被阻止。如果在线程正执行构造函数时挂起它,则 appdomain 中尝试使用该类的其它线程将被阻止。这样容易发生死锁。

方案二:

在 线程运行过程中 适当的位置(如某个完整的功能/命令后)判断是否要继续线程,再决定线程的命运。

1.定义一个全局变量:

?
1
int mtdflag = 0; // 1:正常运行;2:暂停;3:停止

 2. 定义一个判断方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool waitforcontinue()
    {
      if (this.mtdflag == 3)
      {
        return false; // 返回false,线程停止
      }
      else if (this.mtdflag == 2)
      {
        while (mtdflag != 1)
        {
          thread.sleep(200); // 假暂停;停顿时间越短,越灵敏
          if (this.mtdflag == 3)
          {
            return false; // 返回false,线程停止
          }
        }
      }
      return true; // 返回true,线程继续
    }

3.修改 控制命令 事件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private void btn_stop_click(object sender, eventargs e)
   {
     this.mtdflag = 3;
     //mthread.abort(); // 终止
   }
 
   private void btn_suspend_click(object sender, eventargs e)
   {
     this.mtdflag = 2;
     //mthread.suspend(); // 暂停
   }
 
   private void btn_resume_click(object sender, eventargs e)
   {
     this.mtdflag = 1;
     //mthread.resume(); // 继续
   }

4.在线程运行过程中适当的位置,判断线程是否继续

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
mthread = new thread(() =>
     {
       try
       {
         for (int j = 0; j < 20; j++)
         {
           int vsum = 0;
           this.textbox1.text += "--->";
           for (int i = 0; i < 100000000; i++)
           {
             if (i % 2 == 0)
             {
               vsum += i;
             }
             else
             {
               vsum -= i;
             }
             if (i % 10000000 == 0)
             {
               this.textbox1.text += ".";
             }
             if (!waitforcontinue()) // 返回 false 则,停止
             {
               break;
               //return;
             }
           }
           this.textbox1.text += string.format("{0} => vsum = {1}\r\n", datetime.now.tostring(), vsum);
           if (!waitforcontinue()) // 返回 false 则,停止
           {
             break;
             // return;
           }
           thread.sleep(1000);
         }
       }
       catch (threadabortexception ex)
       {
         console.writeline("threadabortexception:{0}", ex.message);
         this.textbox1.text += ex.message + "...";
       }
       finally
       {
         this.textbox1.text += "线程已结束";
       }
     });

在窗体中,解决跨线程访问问题:在窗体构造函数中添加代码:  control.checkforillegalcrossthreadcalls = false;

原文链接:http://www.cnblogs.com/CUIT-DX037/p/6955873.html