C#開啟線程的四種示例
更新時(shí)間:2020年10月12日 11:58:55 作者:zls365
這篇文章主要介紹了C#開啟線程的四種方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
1.異步委托開啟線程
public class Program
{
public static void Main(string[] args)
{
Action<int, int> a = add;
a.BeginInvoke(3, 4, null, null);
Console.WriteLine("執(zhí)行線程");
Console.ReadKey();
}
static void add(int a, int b)
{
Console.WriteLine(a + b);
}
}
2.通過Thread類開啟線程
public class Program
{
public static void Main(string[] args)
{
Thread t1;
Thread t2;
t1 = new Thread(SetInfo1);
t2 = new Thread(SetInfo2);
t1.Start();
//線程睡眠
//t1.Join(1000);
//掛起線程
t1.Suspend();
//繼續(xù)執(zhí)行線程
t1.Resume();
//結(jié)束線程
//t1.Abort();
t2.Start();
Console.ReadKey();
}
//奇數(shù)線程
public static void SetInfo1()
{
for (int i = 0; i < 100; i++)
{
if (i % 2 != 0)
{
Console.WriteLine("奇數(shù)為" + i);
}
}
}
//偶數(shù)線程
public static void SetInfo2()
{
for (int i = 0; i < 100; i++)
{
if (i % 2 == 0)
{
Console.WriteLine("偶數(shù)為" + i);
}
}
}
}
3.通過線程池開啟線程
//線程池可以看做容納線程的容器;一個(gè)應(yīng)用程序最多只能有一個(gè)線程池;ThreadPool靜態(tài)類通過QueueUserWorkItem()方法將工作函數(shù)排入線程池;每排入一個(gè)工作函數(shù),就相當(dāng)于請求創(chuàng)建一個(gè)線程;
//線程池的作用:
//1、線程池是為突然大量爆發(fā)的線程設(shè)計(jì)的,通過有限的幾個(gè)固定線程為大量的操作服務(wù),減少了創(chuàng)建和銷毀線程所需的時(shí)間,從而提高效率。
//2、如果一個(gè)線程的時(shí)間非常長,就沒必要用線程池了(不是不能作長時(shí)間操作,而是不宜。),況且我們還不能控制線程池中線程的開始、掛起、和中止
public class Program
{
public static void Main(string[] args)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(TestThreadPool), new string[] { "hjh" });
Console.ReadKey();
}
public static void TestThreadPool(object state)
{
string[] arry = state as string[];//傳過來的參數(shù)值
int workerThreads = 0;
int CompletionPortThreads = 0;
ThreadPool.GetMaxThreads(out workerThreads, out CompletionPortThreads);
Console.WriteLine(DateTime.Now.ToString() + "---" + arry[0] + "--workerThreads=" + workerThreads + "--CompletionPortThreads" + CompletionPortThreads);
}
}
4.通過任務(wù)Task開啟線程
public class Program
{
public static void Main(string[] args)
{
Task task = new Task(DownLoadFile_My);
task.Start();
Console.ReadKey();
}
static void DownLoadFile_My()
{
Console.WriteLine("開始下載...線程ID:"+Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(500);
Console.WriteLine("下載完成!");
}
}
以上就是C#開啟線程的四種方法匯總的詳細(xì)內(nèi)容,更多關(guān)于C#開啟線程的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
WPF中的ValidationRule實(shí)現(xiàn)參數(shù)綁定解決方案
在WPF中,默認(rèn)情況下,DataContext是通過可視化樹來傳遞的,父元素的DataContext會自動傳遞給其子元素,以便子元素可以訪問父元素的數(shù)據(jù)對象,這篇文章主要介紹了WPF中的ValidationRule實(shí)現(xiàn)參數(shù)綁定解決方案,需要的朋友可以參考下2023-08-08
C#創(chuàng)建簡單windows窗體應(yīng)用(加法器)
這篇文章主要為大家詳細(xì)介紹了C#創(chuàng)建一個(gè)簡單windows窗體應(yīng)用的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
C#實(shí)現(xiàn)Oracle批量寫入數(shù)據(jù)的方法詳解
往數(shù)據(jù)庫批量寫入數(shù)據(jù),這個(gè)功能使用頻率相對還是比較高的,特別是在做一些導(dǎo)入等功能的時(shí)候。本文為大家介紹了C#實(shí)現(xiàn)Oracle批量寫入數(shù)據(jù)的方法,需要的可以參考一下2022-11-11
c# 在windows服務(wù)中 使用定時(shí)器實(shí)例代碼
這篇文章主要介紹了c# 在windows服務(wù)中 使用定時(shí)器實(shí)例代碼,有需要的朋友可以參考一下2013-12-12
C#批量刪除Excel重復(fù)項(xiàng)的實(shí)現(xiàn)方法
當(dāng)從不同來源導(dǎo)入Excel數(shù)據(jù)時(shí),可能存在重復(fù)的記錄,為了確保數(shù)據(jù)的準(zhǔn)確性,通常需要?jiǎng)h除這些重復(fù)的行,本文將提供一個(gè)使用C# 快速查找并刪除Excel重復(fù)項(xiàng)的免費(fèi)解決方案,需要的朋友可以參考下2024-04-04

