zoukankan      html  css  js  c++  java
  • 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
        }
    }
    
    
  • 相关阅读:
    MySQL数据库封装和分页查询
    程序员的价值在哪里?
    奇葩的程序员
    京东咚咚架构演进
    程序员必看的《黑客帝国》,你看懂了吗?
    微信后台技术“干货们”带来的启发
    drf框架 2 drf框架的请求生命周期(as_view和dispatch方法), 请求、解析、渲染、响应、异常, 序列化组件 ,ORM配置回顾(media文件配置),应用在settings.py中INSTALLED_APPS注册意义 ,数据库配置
    drf框架, 接口(api) Django FBV => CBV drf框架的基础试图类 drf核心组件 群查与单查 python换源
    前端Vue框架 05 第三方插件(vuex: 组件间交互的(移动端), axios
    前端Vue框架 04 路由:逻辑跳转、路由传参 项目组件的数据局部化处理data(){ return{} } 组件的生命周期钩子 组件间通信 全局配置css, js
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/1782054.html
Copyright © 2011-2022 走看看