c sharp multithreading

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 testThread
20     {
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}
" + "Thread Name:{1}
" +
21                 "Thread State:{2}
" + "Thread Priority:{3}
", 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 BgTest
26     {
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