zoukankan      html  css  js  c++  java
  • winform --线程

    线程:
    一个进程相当于一个公司,一个进程默认只有一个主线程;
    主线程主要控制窗体,如果你让他执行一段需要点时间的代码,那么窗体就没人控制了,就会出现程序假死情况;

    线程,可以让程序同时干许多事,互相之间不冲突,不排队
    线程启用:
    Thread th = new Thread(需要执行的方法名称);
    th.Start();//可以开始执行

    前台线程:默认,当所有前台线程全部运行完毕后,进程才会结束
    后台线程:当主窗体关闭后,所有后台线程就会自动关闭
    设置方法: th.IsBackground = true;

    -----一般都是后台线程

    关闭线程:th.Abort();
    注意:线程关闭后就无法再次开启了

    ---只能打开新线程

    执行有参数的函数:
    线程只能执行有一个参数,并且没有返回值的函数,而且这个参数必须是object类型;

    参数的传递位置:th.Start(参数);

    ----------手机抽奖------------ 

     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.Windows.Forms;
    10 
    11 namespace 多线程
    12 {
    13     public partial class Form2 : Form
    14     {
    15         public Form2()
    16         {
    17             InitializeComponent();
    18             Control.CheckForIllegalCrossThreadCalls = false;  ------关闭-禁止跨线程访问
    19         }
    20         Thread th = null;
    21         private void button1_Click(object sender, EventArgs e)
    22         {
    23             if (th != null)   ----当不为空时,先关闭线程,再打开新线程
    24             {
    25                 th.Abort();
    26             }
    27             string[] nums = new string[] { "13864311111", "13864322222", "13864333333", "13864344444", "13864355555", "13864366666", };
    28             th = new Thread(suiji);
    29             th.IsBackground = true;
    30             th.Start(nums);  ---带参数
    31         }
    32 
    33 
    34 
    35 public void suiji(object n)------线程只能执行有一个参数,并且没有返回值的函数,而且这个参数必须是object类型;参数的传递位置:th.Start(参数);
    36         {
    37             string[] nums = n as string[];
    38             while (true)
    39             {
    40                 Random r = new Random();
    41                 int ind = r.Next(nums.Length);
    42                 label2.Text = nums[ind];
    43             }
    44         }
    45 
    46         private void button2_Click(object sender, EventArgs e)
    47         {
    48             if (th != null)      -----当不为空时,点击,关闭线程
    49             {
    50                 th.Abort();
    51             }
    52         }
    53 
    54         private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    55         {
    56             th.Abort();  -----关闭程序时,关闭线程
    57         }
    58 
    59 
    60     }
    61 }
  • 相关阅读:
    使用IntelliJ IDEA配置Tomcat(入门)
    关于hive异常:Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStor
    完整过程解决 ERROR 1045 (28000): Access denied for user 'mysql'@'localhost' (using password: NO)
    Hadoop伪分布式配置
    Spark_scala_Maven项目创建
    Spark-WordCount
    tesseract-ocr的安装及使用
    解决selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in P
    python第三方库资源
    kafka伪分布式配置
  • 原文地址:https://www.cnblogs.com/tonyhere/p/5650576.html
Copyright © 2011-2022 走看看