博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c sharp multithreading
阅读量:4555 次
发布时间:2019-06-08

本文共 5965 字,大约阅读时间需要 19 分钟。

1.  静态方法

1 using System; 2 using System.Threading; 3  4 namespace PlusThread 5 { 6     class Program 7     {  8         static void Main(string[] args) 9          {10             //创建无参的线程11             //Thread thread1 = new Thread(new ThreadStart(Thread1));12             Thread thread1 = new Thread( (Thread1));13             thread1.Start();14              Console.ReadLine();15          }16  17          static void Thread1()18           {19               Console.WriteLine("这是无参的方法");20           }21       }22 }
View Code

2.实例方法

1 using System; 2 using System.Threading; 3  4 namespace PlusThread 5 { 6     class Program 7     {  8         static void Main(string[] args) 9         {10             testThread test = new testThread();11             Thread t1 = new Thread(new ThreadStart(test.fun)); 12             t1.Start();13             14             Console.ReadLine();15          }16        17     }18 19     class testThread20     {21         public void fun()22         {23             Console.WriteLine("这是实例方法");24         }25     }26 27 }
View Code

简洁写法:

1 using System; 2 using System.Threading; 3  4 namespace PlusThread 5 { 6     class Program 7     {  8         static void Main(string[] args) 9         {10              11             Thread t1 = new Thread(delegate() { Console.WriteLine("匿名委托创建线程"); });12             Thread t2 = new Thread(()=> { Console.WriteLine("lambda创建线程"); Console.WriteLine("hello"); });13             t1.Start();14             t2.Start();15             Console.ReadLine();16          }       17     }18 }
View Code

3. 带参数实例

1 using System; 2 using System.Threading; 3  4 namespace PlusThread 5 { 6     class Program 7     { 8         static void Main(string[] args) 9         {             10             Thread t1 = new Thread(new ParameterizedThreadStart(testThread ));11             t1.Start();12             Console.ReadLine();13         }14         static void testThread(object obj)15         {16             Console.WriteLine("带参数实例");17         }18     } 19 }
View Code

4. 线程基本信息

1 using System; 2 using System.Threading; 3  4 namespace PlusThread 5 { 6     class Program 7     { 8         static void Main(string[] args) 9         {10             //获取正在运行的线程11             Thread thread = Thread.CurrentThread;12             //设置线程的名字13             thread.Name = "主线程";14             //获取当前线程的唯一标识符15             int id = thread.ManagedThreadId;16             //获取当前线程的状态17             ThreadState state = thread.ThreadState;18             //获取当前线程的优先级19             ThreadPriority priority = thread.Priority;20             string strMsg = string.Format("Thread ID:{0}\n" + "Thread Name:{1}\n" +21                 "Thread State:{2}\n" + "Thread Priority:{3}\n", id, thread.Name,22                 state, priority);23 24             Console.WriteLine(strMsg);25 26             Console.ReadKey();27         }28     } 29 }
View Code

5. 前后台线程

1 using System; 2 using System.Threading; 3  4 namespace PlusThread 5 { 6     class Program 7     { 8         static void Main(string[] args) 9         {10             BgTest bg = new BgTest(10);11             Thread fThread = new Thread(new ThreadStart(bg.Run));12             fThread.Name = "前台线程";13 14             BgTest bg1 = new BgTest(20);15             Thread bThread = new Thread(new ThreadStart(bg1.Run));16             bThread.Name = "后台线程";17             bThread.IsBackground = true;18 19             fThread.Start();20             bThread.Start();21             Console.ReadLine();22         }23     }24 25     class BgTest26     {27         private int Count;28         public BgTest(int count)29         {30             this.Count = count;31         }32         public void Run()33         {34             string threadName = Thread.CurrentThread.Name;35             for (int i = 0; i < Count; i++)36             {37                 Console.WriteLine("{0}计数:{1}", threadName, i.ToString());38                 Thread.Sleep(1000);39             }40             Console.WriteLine("{0}完成计数", threadName);41         }42     }43 }
View Code

6. 跨线程访问控件

6.1 

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Threading;namespace WindowsFormsApp1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();            Control.CheckForIllegalCrossThreadCalls = false;        }        private void button1_Click(object sender, EventArgs e)        {            Thread t = new Thread(test);            t.Start();            Console.ReadLine();            void test()            {                for(int i=0;i<10;i++)                {                    textBox1.Text = i.ToString();                    Thread.Sleep(200);                }            }        }    }}
View Code

6.2 

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Threading;namespace WindowsFormsApp1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private delegate void setCallBack(int value);        private setCallBack setcb;        private void button1_Click(object sender, EventArgs e)        {            setcb = new setCallBack(setNum);            Thread t = new Thread (test);            t.Start();            void test()            {                for(int i=0;i<10;i++)                {                    textBox1.Invoke(setcb, i);                }            }            void setNum(int i)            {                textBox1.Text = i.ToString();                Thread.Sleep(500);            }        }    }}
View Code

7. 

 

参考:

https://www.cnblogs.com/dotnet261010/p/6159984.html

转载于:https://www.cnblogs.com/https/p/9345394.html

你可能感兴趣的文章
MySQL用户与权限管理
查看>>
JavaScript中清空数组的三种方式
查看>>
Atitit机器学习原理与概论book attilax总结
查看>>
Atitit 深入理解耦合Coupling的原理与attilax总结
查看>>
Ch06 验证
查看>>
MyEclipse10采用links安装插件的方法
查看>>
C#部分特殊符号和关键字使用小技巧
查看>>
WinForm编程数据视图之DataGridView浅析(续)
查看>>
16、Django实战第16天:优化url
查看>>
TextView 单行 ellipsize
查看>>
【Python图像特征的音乐序列生成】第一阶段的任务分配
查看>>
Storm入门(2)--Storm编程
查看>>
任正非:华为3年前应该快垮了
查看>>
爬虫实例——爬取淘女郎相册(通过selenium、PhantomJS、BeautifulSoup爬取)
查看>>
Random随机类
查看>>
Git学习笔记 1,GitHub常用命令1
查看>>
详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别
查看>>
php引用计数与变量引用
查看>>
UITableView移动
查看>>
linux 查看自己所在的公网ip
查看>>