C#线程

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

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
       RichTextBox TextBox=new RichTextBox();
        public Form1()
        {
            InitializeComponent();
            TextBox = richTextBox1;

           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //获取所有计算机的进程
            Process[] processes = Process.GetProcesses();
            foreach (Process p in processes)
            {
                richTextBox1.Text = richTextBox1.Text + p.ProcessName + "\r\n";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Process[] processes = Process.GetProcesses();
            foreach (Process p in processes)
            {
               if(p.ProcessName== "notepad")
                {
                    //p.Close();
                    //判断进程是否处于运行状态
                    if (!p.HasExited)
                    {
                        p.Kill();
                    }
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //获取进程名称
            //string ProcessName = "mspaint";
            string ProcessName = "notepad";
            //创建Process 类的对象
            Process p = new Process();
            //设置进程名称
            p.StartInfo.FileName = ProcessName;
            //启动进程
            p.Start();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ThreadStart childref = new ThreadStart(CallToChildThread);
          //  Console.WriteLine("In Main: Creating the Child thread");
            Thread childThread = new Thread(childref);
            childThread.Start();
           // Console.ReadKey();

        }

        public  void CallToChildThread()
        {
            // 加入这行,可以跨线程赋值给空间
             Control.CheckForIllegalCrossThreadCalls = false;
            // Console.WriteLine("Child thread starts");
            Thread.Sleep(1000);//延迟1秒
            TextBox.Text = "创建线程完成";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Thread thread1 = new Thread(new ParameterizedThreadStart(UpdateLabel2));
            thread1.Start("更新Label");
        }

        private void UpdateLabel2(object str)
        {
            if (TextBox.InvokeRequired)
            {
                // 当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它
                Action<string> actionDelegate = (x) => { this.TextBox.Text = x.ToString(); };
                // 或者
                // Action<string> actionDelegate = delegate(string txt) { this.label2.Text = txt; };
                this.TextBox.Invoke(actionDelegate, str);
            }
            else
            {
                this.TextBox.Text = str.ToString();
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            using (BackgroundWorker bw = new BackgroundWorker())
            {
                bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
                bw.DoWork += new DoWorkEventHandler(bw_DoWork);
                bw.RunWorkerAsync("Tank");
            }
        }

        void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            // 这里是后台线程, 是在另一个线程上完成的
            // 这里是真正做事的工作线程
            // 可以在这里做一些费时的,复杂的操作
            Thread.Sleep(5000);
            e.Result = e.Argument + "工作线程完成";
        }

        void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //这时后台线程已经完成,并返回了主线程,所以可以直接使用UI控件了 
            this.TextBox.Text = e.Result.ToString();
        }
    }
}

----------------------

 

1.线程通信:

using System;
using System.Threading;

namespace ConsoleApp1
{
    delegate void MyDel(string value);
    class Program
    {
        //若要将初始状态设置为终止,则为 true;若要将初始状态设置为非终止,则为 false
        static AutoResetEvent AutoResetEvent = new AutoResetEvent(false);

        static void Main(string[] args)
        {
            Thread t1
                = new Thread(() =>
                 {
                     Console.WriteLine("等待付款。。。");
                     AutoResetEvent.WaitOne();
                     Console.WriteLine("发来支付通知!");

                 });
            t1.IsBackground = true;
            t1.Start();
            Pay();

           
        }

        private static void Pay()
        {
            string tip = Console.ReadLine();
            if(tip == "123")
            {
                AutoResetEvent.Set();//停止线程
            }
        }
    }
}
https://www.cnblogs.com/haosola/p/3674609.html

 

-----

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

namespace ConsoleApp1
{
    delegate void MyDel(string value);
    class Program
    {
       static List<AutoResetEvent> autoResetEvents = new List<AutoResetEvent>();
        static void Main(string[] args)
        {
            

            Thread thread = new Thread(new ParameterizedThreadStart(ThreadMethod));
            thread.Name = "111111";
            object obj = 10;
            thread.Start(obj);

            Thread thread2 = new Thread(new ParameterizedThreadStart(ThreadMethod2));
            thread2.Name = "339247";
            object obj2 = 10;
            thread2.Start(obj2);


        }

        //发出通知 1
        public static void ThreadMethod(object number)
        {
             AutoResetEvent AutoResetEvent = new AutoResetEvent(false);

            int i = (int)number;
       
       
                Console.WriteLine("当前线程ID:{0},number={1}", Thread.CurrentThread.ManagedThreadId, i);

         
            autoResetEvents[0].Set();

                AutoResetEvent.WaitOne(TimeSpan.FromSeconds(5));
                Console.WriteLine("结束当前线程ID:{0},number={1}", Thread.CurrentThread.ManagedThreadId, i);


            
        }

        //异步线程通知 1结束线程
        public static void ThreadMethod2(object number)
        {
            AutoResetEvent AutoResetEvent = new AutoResetEvent(false);
            autoResetEvents.Add(AutoResetEvent);
            int i = (int)number;
 
                Console.WriteLine("当前线程ID:{0},number={1}", Thread.CurrentThread.ManagedThreadId, i);

                AutoResetEvent.WaitOne();
                Console.WriteLine("结束当前线程ID:{0},number={1}", Thread.CurrentThread.ManagedThreadId, i);


            
        }

    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智慧方

开发程序不易

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值