C# 线程练习1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace TreadEx
{
    class Class1
    {
        public volatile bool shouldstop;
        private Form1 form1;
        public Class1(Form1 form1)
        {
            this.form1 = form1;
        }
        public void Method1(object obj)
        {
            string s = obj as string;
            form1.AddMessage(s);
            while (shouldstop==false)
            {
                Thread.Sleep(100);
                form1.AddMessage("a");
            }
            form1.AddMessage("\n 线程Method1已终止");
        }
        public void Method2()
        {
            while (shouldstop == false)
            {
                Thread.Sleep(100);
                form1.AddMessage("b");
            }
            form1.AddMessage("\n 线程Method2已终止");
        }
    }
}



委托button1_Click订阅事件button1.Click,就执行1次button1_click
订阅了两次,执行两次,故产生了4个线程。




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace TreadEx
{
    public partial class Form1 : Form
    {
        Thread thread1, thread2;
        Class1 class1;
        public Form1()
        {
            InitializeComponent();
            class1 = new Class1(this);
            button1.Click+=new EventHandler(button1_Click);//委托button1_Click订阅事件button1.Click,即执行1次
            button1.Click+=new EventHandler(button1_Click);//订阅了两次,执行两次,故产生了4个线程
button2.Click+=new EventHandler(button2_Click); } private void button1_Click(object sender, EventArgs e) { richTextBox1.Clear(); class1.shouldstop = false; thread1 = new Thread(class1.Method1); thread1.IsBackground = true; thread2 = new Thread(class1.Method2); thread2.IsBackground = true; thread1.Start("A thread start \n"); thread2.Start(); } private void button2_Click(object sender, EventArgs e) { //class1.shouldstop = true; thread1.Abort(); //thread1.Join(); thread2.Abort(); //thread2.Join(); } private delegate void AddMessageDelegate(string message); public void AddMessage(string message) { if (richTextBox1.InvokeRequired) { AddMessageDelegate d = AddMessage; richTextBox1.Invoke(d, message); } else { richTextBox1.AppendText(message); } } }}

由于thread1,thread2指向后生成的两个线程,所以通过thread1,thread2只能控制后两个线程,前生成的两个线程仍然运行。
结果:点击button2前输出aabb,点击button2后会继续输出a和b。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace TreadEx
{
    public partial class Form1 : Form
    {
        Thread thread1, thread2;
        Class1 class1;
        public Form1()
        {
            InitializeComponent();
            class1 = new Class1(this);
            button1.Click+=new EventHandler(button1_Click);
            button2.Click+=new EventHandler(button2_Click);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            class1.shouldstop = false;
            thread1 = new Thread(class1.Method1);
            thread1.IsBackground = true;
            thread2 = new Thread(class1.Method2);
            thread2.IsBackground = true;
            thread1.Start("A thread start \n");
            thread2.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            class1.shouldstop = true;
            //thread1.Abort();
            //thread1.Join();
            //thread2.Abort();
            //thread2.Join();
        }

        private delegate void AddMessageDelegate(string message);

        public void AddMessage(string message)
        {
            if (richTextBox1.InvokeRequired)
            {
                AddMessageDelegate d = AddMessage;
                richTextBox1.Invoke(d, message);
            } 
            else
            {
                richTextBox1.AppendText(message);
            }
            
        }
    }
}
结果:点击Button2后,四个线程全部结束。

说明,使用volatile控制线程,比thread.abort更安全。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值