线程:主线程、子线程 同步线程、异步线程 单线程、多线程 System.Threading与System.Windows.Threading

时间:2022-08-06 12:57:36

入门--------------------------------------------------------------------------------
  概述与概念
    一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为“主线程”)自动创建。

  创建和开始使用多线程
    public Window1()
    {
        //主线程
        //Code……
        //使用匿名方法来启动子线程
        Thread t = new Thread(delegate() {Code……});
        t.Start();
    }

子进程创建实例
    //1.创建子线程
    public Window1()
    {
        //创建子进程
        Thread threadA = new Thread(new ThreadStart(WorkMethod));
        threadA.Start();
    }
    //子进程启动程序
    void WorkMethod()
    {
        this.textBox1.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate
        {
            textBox1.AppendText(Environment.NewLine);
            this.textBox1.AppendText("子线程执行完成!");
        });
    }

//2.使用匿名委托来启动子线程
    public Window1()
    {
        Thread t = new Thread(delegate() {
            this.textBox1.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate
            {
                textBox1.AppendText(Environment.NewLine);
                this.textBox1.AppendText("子线程执行完成!");
            });
        });
        t.Start();
    }

//3.使用匿名委托来启动子线程
    public Window1()
    {
        InitializeComponent();
        textBox1.AppendText("主线程执行完成");
        new Thread(() =>
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                textBox1.AppendText(Environment.NewLine);
                textBox1.AppendText("子线程执行完成");
            }));
        }).Start();
    }

线程同步基础--------------------------------------------------------------------------------
  同步要领
  锁和线程安全
    //将当前线程阻塞指定的时间
    Thread.Sleep(TimeSpan.FromSeconds(30));

  Interrupt 和 Abort
  线程状态
  等待句柄
  同步环境

使用多线程--------------------------------------------------------------------------------
  单元模式和Windows Forms
  BackgroundWorker类
  ReaderWriterLock类
  线程池
  异步委托
  计时器
    public Window1()
    {
        //计时器 SetTimeInterval
        System.Windows.Threading.DispatcherTimer t = new System.Windows.Threading.DispatcherTimer();
        t.Interval = new TimeSpan(0, 0, 0, 0, 100);
        t.Tick += new EventHandler(timer_Interval);
        t.Start();
    }
    public Window1()
    {
        //计时器 SetTimeOut
        new System.Threading.Timer(new TimerCallback(timer_Callback), this, 5000, 0);
    }
  局部储存

高级话题--------------------------------------------------------------------------------
  非阻止同步
  Wait和Pulse
  Suspend和Resume
  终止线程

同步线程实例
    public Window1()
    {
        InitializeComponent();
        this.WindowStartupLocation = WindowStartupLocation.CenterOwner;

this.textBox1.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate
        {
            textBox1.AppendText(Environment.NewLine);
            this.textBox1.AppendText("同步线程执行完成!");
        });
    }
    异步线程实例
    public Window1()
    {
        textBox1.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
        {
            textBox1.AppendText(Environment.NewLine);
            textBox1.AppendText("异步线程执行完成");
        }));
    }
    线程呈现控件实例-可传参数
    public Window1()
    {
        InitializeComponent();
        System.Windows.Threading.DispatcherTimer t = new System.Windows.Threading.DispatcherTimer();
        t.Interval = new TimeSpan(0, 0, 0, 0, 1000);
        t.Tick += new EventHandler(Dispatcher_Timer);
        t.Start();
    }
    private delegate void DispatcherDelegate(string msg);
    private void OutPut(string msg)
    {
        textBox1.Dispatcher.Invoke(new DispatcherDelegate(DelegateMethod), msg);
    }
    private void DelegateMethod(string msg)
    {
        textBox1.AppendText(msg);
        textBox1.AppendText(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
        textBox1.AppendText(Environment.NewLine);
    }
    void Dispatcher_Timer(object sender, EventArgs e)
    {
        OutPut("控件呈现:");
    }
    线程呈现控件实例-使用匿名委托
    public Window1()
    {
        InitializeComponent();
        this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
        System.Windows.Threading.DispatcherTimer t = new System.Windows.Threading.DispatcherTimer();
        t.Interval = new TimeSpan(0, 0, 0, 0, 1000);
        t.Tick += new EventHandler(Dispatcher_Timer);
        t.Start();
    }
    void Dispatcher_Timer(object sender, EventArgs e)
    {
        textBox1.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
        {
            textBox1.AppendText("控件呈现:");
            textBox1.AppendText(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
            textBox1.AppendText(Environment.NewLine);
        }));
    }
    线程呈现控件实例-匿名方法并返回参数
    public Window1()
    {
        InitializeComponent();
        System.Windows.Threading.DispatcherTimer t = new System.Windows.Threading.DispatcherTimer();
        t.Interval = new TimeSpan(0, 0, 0, 0, 1000);
        t.Tick += new EventHandler(Dispatcher_Timer);
        t.Start();
    }
    void Dispatcher_Timer(object sender, EventArgs e)
    {
        string strmsg = (string)textBox1.Dispatcher.Invoke(new Func<string>(() =>
        {
            textBox1.AppendText("控件呈现:");
            textBox1.AppendText(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
            textBox1.AppendText(Environment.NewLine);
            return textBox1.Text;
        }));
    }