zoukankan      html  css  js  c++  java
  • C#多线程

    命名空间 System.Threading
    //创建多线程
    Thread (线程实例名称thd) =new Thread(new ThredStart(方法名称)); //方法名称指写一个方法调用
    thd.Start(); //开始执行
    --------------------------------------------------------------------------------------------
    多线程属性
    CurrentThread //获取当前运行的线程
    IsAlive //获取当前现在执行状态值
    Name //获取线程名称
    Priority //设置或获取线程优先值
    ThreadState //获取包含当前线程的状态值
    ----------------------------------------------
    多线程方法
    Start(); //执行多线程
    Sleep(); //禁止线程运行的毫秒数 睡眠
    Abort(); //调用该方法终止线程
    Join(); //禁止调用线程,直到线程终止
    Thread.Interrupt(); //中止处于Sleep或者join线程状态的线程
    Suspend(); //挂起线程;如果线程已经挂断,则方法无效
    Resume(); //恢复被挂起的线程
    锁定
    lock(this)
    {
    //代码
    }
    -------------------------------------------------------------
    摇奖机
    ----------------------------------
    namespace WindowsApplication2
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    //2005 bug需要设置
    Control.CheckForIllegalCrossThreadCalls = false;
    this.label1.Text = "猜";
    this.label2.Text = "猜";
    this.label3.Text = "猜";
    //设置转换为字符串
    this.label4.Text = b.ToString();
    this.textBox1.Text = "100";
    }
    int b = 54561;
    //随机
    Random a = new Random();
    //鼠标点击事件
    private void button1_Click(object sender, EventArgs e)
    {
    //比较输入金额与金库金额
    if (int.Parse(this.label4.Text.ToString()) {
    MessageBox.Show("金额不够");
    return;
    }
    //创建多线程 并未执行
    Thread thd1 = new Thread(new ThreadStart(Run1));
    Thread thd2 = new Thread(new ThreadStart(Run2));
    Thread thd3 = new Thread(new ThreadStart(Run3));
    Thread thd4 = new Thread(new ThreadStart(Run4));
    //执行多线程
    thd1.Start();
    thd2.Start();
    thd3.Start();
    thd4.Start();
    }
    //方法
    public void Run1()
    {
    //循环产生随机数字
    for(int i=0;i<=100;i++)
    {
    this.label1.Text = a.Next(10).ToString();
    //随机睡眠
    Thread.Sleep(a.Next(10));
    i++;
    }
    }
    public void Run2()
    {
    for (int i = 0; i <= 100; i++)
    {
    this.label2.Text = a.Next(10).ToString();
    Thread.Sleep(a.Next(10));
    i++;
    }
    }
    public void Run3()
    {
    for (int i = 0; i <= 100; i++)
    {
    this.label3.Text = a.Next(10).ToString();
    Thread.Sleep(a.Next(10));
    i++;
    }
    }
    //控制多线程方法
    public void Run4()
    {
    for (int i = 0; i <= 100; i++)
    {
    if (i == 100)
    {
    //判断3个随机数是否相同
    if (this.label1.Text == this.label2.Text && this.label2.Text==this.label3.Text)
    {
    //相同增加金额10倍
    b = b + int.Parse(textBox1.Text.ToString())*10;
    this.label4.Text = b.ToString();
    }
    //不同 减少金额
    b = b - int.Parse(textBox1.Text.ToString());
    this.label4.Text = b.ToString();
    }
    }
    }
    }
    }
    ------------------------------------------------------------------------------------------------------------
    多线程 质数线程的控制
    -----------------------
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    //导包
    using System.Threading;
    namespace WindowsApplication1
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    private Thread thd;

    private void Form1_Load(object sender, EventArgs e)
    {
    Control.CheckForIllegalCrossThreadCalls = false;
    this.button2.Enabled = false;
    this.button3.Enabled = false;
    this.button4.Enabled = false;

    }
    //停止按钮
    private void button4_Click(object sender, EventArgs e)
    {
    this.button4.Enabled = false;
    this.button1.Enabled = true;
    this.button2.Enabled = true;
    //清除线程
    thd.Abort();
    }

    private void button2_Click(object sender, EventArgs e)
    {
    try
    {
    //如果线程的当前状态为运行或休眠等待,则暂停线程
    //ThreadState.Running线程状态已启动
    if (thd.ThreadState==ThreadState.Running || thd.ThreadState== ThreadState.WaitSleepJoin)
    {
    //暂停线程
    thd.Suspend();
    //禁用暂停按钮
    this.button2.Enabled = false;
    //启用恢复按钮
    this.button3.Enabled = true;
    //禁用停止按钮
    this.button4.Enabled = false;
    }
    }
    catch (ThreadStartException msg)
    {
    //输出异常信息
    MessageBox.Show(msg.ToString(),"异常");
    }
    }

    public void Generate()
    {
    //true=质数,false=非质数
    bool Isprime = true;
    //存放质数的个数
    int count = 2;
    //第一个数是质数,添加列表中
    this.listBox1.Items.Add(2);
    //执行循环生成另外24个质数
    for (int i = 3; count < 50; i+=2)
    {
    Isprime = true;
    //循环次数是要检查的数字"i"的一半
    for (int j = 2; j <=(i/2); j++)
    {
    //检查"i"是否可以被整除
    if (i%j==0)
    {
    Isprime=false;
    break;
    }
    }
    //显示质数并递增计算器
    if (Isprime)
    {
    this.listBox1.Items.Add(i);
    count++;
    //使该线程休眠100毫秒,以启用线程的暂停和恢复
    Thread.Sleep(100);
    }
    }
    //线程完成执行后,启用和禁用这些按钮
    this.button1.Enabled=true;
    this.button2.Enabled=false;
    this.button3.Enabled=false;
    this.button4.Enabled=false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
    //创建线程
    thd = new Thread(new ThreadStart(Generate));
    //为线程指定名称
    thd.Name = "质数";
    //启用暂停按钮
    this.button2.Enabled = true;
    //禁用开始按钮
    this.button1.Enabled = false;
    //启用停止按钮
    this.button4.Enabled = true;
    //初始化线程
    //执行线程
    thd.Start();
    }

    private void button3_Click(object sender, EventArgs e)
    {
    //线程状态ThreadState
    //Suspended 已挂断
    //SuspendRequested 正在请求挂断
    //WaitSleepJoin 线程已北阻止
    //Running 线程已启动
    //Abort(); 清除线程
    //Suspend(); 暂停进程
    //Resume(); 恢复进程
    if (this.thd.ThreadState== ThreadState.Suspended || thd.ThreadState== ThreadState.SuspendRequested || thd.ThreadState== ThreadState.WaitSleepJoin || Convert.ToInt32(thd.ThreadState)==96)
    {
    try
    {
    //恢复线程
    thd.Resume();
    //禁用恢复按钮
    this.button3.Enabled = false;
    //启用暂停按钮
    this.button2.Enabled = true;
    //启用停止按钮
    this.button4.Enabled = true;
    }
    catch (ThreadStartException msg)
    {
    MessageBox.Show(msg.ToString(), "异常");
    }
    }
    }
    }
    }

    固定链接 评论(0)┆阅读(230)
  • 相关阅读:
    poj 2349 Arctic Network
    hdu 1596 find the safest road
    Codeforces 768B. Code For 1
    Codeforces 448C. Painting Fence
    Problem D. Ice Cream Tower(2016 China-Final)
    poj 2785 4 Values whose Sum is 0
    Codeforces 797C. Minimal string
    Codeforces 264A. Escape from Stones
    乌龟棋(noip2010)
    noip2018模拟题(类背包+贪心)
  • 原文地址:https://www.cnblogs.com/luluping/p/1575692.html
Copyright © 2011-2022 走看看