Thread Examples

Code:

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

namespace WindowsFormsApp
{
    /// <summary>
    /// 多线程学习笔记
    /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //DoWithEasy();
            //DoWithParameter();
            //DoWithTimer();
            //DoWithThreadPool();
            //DoWithThreadPoolParameter();
            //DoWithAnonymous();
            //DoWithLambda();
            //DoWithCommon();
            //DoWithAction();
            //DoWithFunc();
            DoWithPredicate();
        }

        #region A simple Thread
        private void DoWithEasy()
        {
            Thread t = new Thread(new ThreadStart(this.DoSomethingWithEasy));
            t.Start();
        }
        private void DoSomethingWithEasy()
        {
            MessageBox.Show("A simple Thread");
        }
        #endregion

        #region A simple Thread with parameter
        private void DoWithParameter()
        {
            Thread t = new Thread(new ParameterizedThreadStart(this.DoSomethingWithParameter));
            t.Start("Thread with parameter");
        }
        private void DoSomethingWithParameter(object x)
        {
            MessageBox.Show(x.ToString());
        }
        #endregion

        #region Thread with Timer
        public void DoWithTimer()
        {
            System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
            timer.Interval = 1000;//定时器触发每隔一秒钟。
            timer.Tick += (x, y) =>
                {
                    MessageBox.Show("timer thread.");
                };
            timer.Start();
        }
        #endregion

        #region ThreadPool with no parameter
        private void DoWithThreadPool()
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(this.DoSomethingWithThreadPoolNO));
        }

        private void DoSomethingWithThreadPoolNO(object x)
        {
            MessageBox.Show("ThreadPool");
        }
        #endregion

        #region ThreadPool with parameter
        private void DoWithThreadPoolParameter()
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(this.DoSomethingWithThreadPoolParameter),"ThreadPool Parameter");
        }
        private void DoSomethingWithThreadPoolParameter(object x)
        {
            MessageBox.Show(x.ToString());
        }
        #endregion

        #region ThreadPool with Anonymous delegate
        private void DoWithAnonymous()
        {
            //匿名委托
            ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object x)
                {
                    MessageBox.Show("ThreadPool Anonymous delegate.");
                }));
        }
        #endregion

        #region ThreadPool with lambda
        private void DoWithLambda()
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(x => 
                {
                    MessageBox.Show("ThreadPool Lambda");
                }));
        }
        #endregion

        #region Thread change UI
        private void DoWithCommon()
        {
            WaitCallback waitcallback = new WaitCallback(this.InvokeMethod);
            ThreadPool.QueueUserWorkItem(waitcallback," Thread change UI.");
        }
        private delegate void InvokeMethodDelegate(string name);
        private void InvokeMethod(object x)
        {
            this.Invoke(new InvokeMethodDelegate(this.ChangeUIWithCommon),x.ToString());
        }
        private void ChangeUIWithCommon(string name)
        {
            this.lblMessage.Text = name;
        }     
        #endregion

        #region Thread change UI with Action
        private void DoWithAction()
        {
            WaitCallback waitcallback = new WaitCallback(this.DoSomethingWithAction);
            ThreadPool.QueueUserWorkItem(waitcallback, "Thread change UI with Action");
        }
        private void DoSomethingWithAction(object x)
        {
            this.Invoke(new Action<string>(this.ChangeUI),x.ToString());
        }
        private void ChangeUI(string message)
        {
            this.lblMessage.Text = message;
        }
        #endregion

        #region Thread change UI with Func
        private void DoWithFunc()
        {
            WaitCallback waitcallback = new WaitCallback(this.DoSomethingWithFunc);
            ThreadPool.QueueUserWorkItem(waitcallback,"Test Func");
        }
        private void DoSomethingWithFunc(object x)
        {
            Func<string, int> f = new Func<string, int>(this.GetFuncMessage);
            object result = this.Invoke(f,x.ToString());
            MessageBox.Show(result.ToString());
        }
        private int GetFuncMessage(string message)
        {
            this.lblMessage.Text = message;
            if (message == "Test Func")
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        #endregion

        #region Thread change UI with Predicate
        private void DoWithPredicate()
        {
            WaitCallback waitcallback = new WaitCallback(this.DoSomethingWithPredicate);
            ThreadPool.QueueUserWorkItem(waitcallback, "Predicate");
        }
        private void DoSomethingWithPredicate(object x)
        {
            Predicate<string> pd = new Predicate<string>(this.GetPredicateMessage);
            object result = this.Invoke(pd,x);
            MessageBox.Show(result.ToString());
        }
        private bool GetPredicateMessage(string message)
        {
            this.lblMessage.Text = message;
            if (message == "Predicate")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion
    }
}

原文地址:https://www.cnblogs.com/Leo_wl/p/1782054.html