• WinForm窗体多线程操作实例


    最近在学习C# 多线程相关知识,这块一直比较薄弱,在网上查了一下资料,学习了一下前辈们的经验,小弟自己也比葫芦画瓢的写了一个,自学一下。

    代码如下

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Threading;
      9 using System.Threading.Tasks;
     10 using System.Windows.Forms;
     11 
     12 namespace WindowsFormsApp1
     13 {
     14     public partial class Form1 : Form
     15     {
     16         public Form1()
     17         {
     18             InitializeComponent();
     19         }
     20 
     21         
     22 
     23         //声明线程数组1
     24         Thread[] thread1 = new Thread[1];
     25 
     26         //声明线程数组2
     27         Thread[] thread2 = new Thread[1];
     28 
     29 
     30 
     31         //声明委托,为listbox1增加Item
     32         delegate void AddItemMethod1(string txt);
     33 
     34         //声明委托,为listbox2增加Item
     35         delegate void AddItemMethod2(string txt);
     36 
     37         //利用线程为Listbox1添加内容
     38         private void AddItem1(string txt)
     39         {
     40             if (this.InvokeRequired)
     41             {
     42                 AddItemMethod1 d = new AddItemMethod1(AddItem1);
     43                 this.Invoke(d, new object[] { txt });
     44             }
     45             else
     46             {
     47                 this.listBox1.Items.Add(txt);
     48             }
     49         }
     50 
     51         //利用线程为Listbox2添加内容
     52         private void AddItem2(string txt)
     53         {
     54             if (this.InvokeRequired)
     55             {
     56                 AddItemMethod2 d = new AddItemMethod2(AddItem2);
     57                 this.Invoke(d, new object[] { txt });
     58             }
     59             else
     60             {
     61                 this.listBox2.Items.Add(txt);
     62             }
     63         }
     64 
     65         //生成Item
     66         public void GetItem1()
     67         {
     68             while (true)
     69             {
     70                 AddItem1("OK");
     71                 Thread.Sleep(1000);
     72             }
     73         }
     74 
     75         //生成Item
     76         public void GetItem2()
     77         {
     78             while (true)
     79             {
     80                 AddItem2("NICE");
     81                 Thread.Sleep(5000);
     82             }
     83         }
     84 
     85         private void button1_Click(object sender, EventArgs e)
     86         {
     87             ////循环创建并启动线程1
     88             for (int i = 0; i < thread1.Length; i++)
     89             {
     90                 if (thread1[i] == null)//线程不存在
     91                 {
     92                     thread1[i] = new Thread(new ThreadStart(() =>
     93                     {
     94                         while (true)
     95                         {
     96                             AddItem1("OK");
     97                             Thread.Sleep(1000);
     98                             //listbox1 的Item数量到20个是停止thread1
     99                             if (listBox1.Items.Count >10)
    100                             {
    101 
    102                                 if (i >= thread1.Length)
    103                                 {
    104                                     i = thread1.Length - 1;
    105                                 }
    106                                 //如果不加上面的判断,下面就会提示索引超出数组长度界限,我定义了thread1 的线程个数是1个,for循环中i为1的情况应该是进不来的,为啥
    107                                 // 下面的i会是1??
    108                                 thread1[i].Abort();
    109                             }
    110                         }
    111                     }));
    112                     thread1[i].Name = i.ToString();
    113                     thread1[i].Start();
    114                 }
    115                 else
    116                 {
    117                     //已经存在,如果没有运行,则启动  
    118                     if (thread1[i].ThreadState == ThreadState.Aborted || thread1[i].ThreadState == ThreadState.Stopped)
    119                     {
    120                         thread1[i] = new Thread(new ThreadStart(GetItem1));
    121                         thread1[i].Name = i.ToString();
    122                         thread1[i].Start();
    123                     }
    124                     else
    125                     {
    126                         thread1[i].Start();
    127                     }
    128                 }
    129             }
    130 
    131 
    132             ////循环创建并启动线程2
    133             for (int j = 0; j < thread2.Length; j++)
    134             {
    135                 if (thread2[j] == null)//线程不存在
    136                 {
    137                     thread2[j] = new Thread(new ThreadStart(GetItem2));
    138                     thread2[j].Name = j.ToString();
    139                     thread2[j].Start();
    140                 }
    141                 else
    142                 {
    143                     //已经存在,如果没有运行,则启动  
    144                     if (thread2[j].ThreadState == ThreadState.Aborted || thread2[j].ThreadState == ThreadState.Stopped)
    145                     {
    146                         thread2[j] = new Thread(new ThreadStart(GetItem2));
    147                         thread2[j].Name = j.ToString();
    148                         thread2[j].Start();
    149                     }
    150                     else
    151                     {
    152                         thread2[j].Start();
    153                     }
    154                 }
    155             }
    156         }
    157 
    158         private void button2_Click(object sender, EventArgs e)
    159         {
    160             //循环停止线程1执行  
    161             for (int i = 0; i < thread1.Length; i++)
    162             {
    163                 //如果线程存在,并且状态不是停止也不是终止的,则终止该线程  
    164                 if (thread1[i] != null && thread1[i].ThreadState != ThreadState.Stopped && thread1[i].ThreadState != ThreadState.Aborted)
    165                 {
    166                     thread1[i].Abort();
    167                 }
    168             }
    169 
    170             //循环停止线程2执行  
    171             for (int j = 0; j < thread2.Length; j++)
    172             {
    173                 //如果线程存在,并且状态不是停止也不是终止的,则终止该线程  
    174                 if (thread2[j] != null && thread2[j].ThreadState != ThreadState.Stopped && thread2[j].ThreadState != ThreadState.Aborted)
    175                 {
    176                     thread2[j].Abort();
    177                 }
    178             }
    179         }
    180     }
    181 }
    View Code
  • 相关阅读:
    JVM指令
    spring源码分析之配置文件名占位符的解析(一)
    freemarker
    spring整合freemarker
    策略模式
    spring boot 学习笔记(一)之前端文件配置
    线程使用总结
    maven pom 配置 学习笔记(二)之搭建nexus私服
    删除数据库中所有存在表的数据
    自定义SWT控件七之自定义Shell(可伸缩窗口)
  • 原文地址:https://www.cnblogs.com/zyfadmin/p/8395216.html
Copyright © 2020-2023  润新知