C#使用Task.ContinueWith組合任務(wù)
代碼案例
簡(jiǎn)單Demo
代碼:
public static void Main()
{
//創(chuàng)建一個(gè)任務(wù)
Task<int> task = new Task<int>(() =>
{
int sum = 0;
Console.WriteLine("使用Task異步執(zhí)行操作.");
for (int i = 0; i <= 100; i++)
{
sum += i;
}
return sum;
});
//啟動(dòng)任務(wù),并安排到當(dāng)前任務(wù)隊(duì)列線程中執(zhí)行任務(wù)(System.Threading.Tasks.TaskScheduler)
task.Start();
Console.WriteLine("主線程執(zhí)行其他程序.");
//任務(wù)完成時(shí)執(zhí)行處理。
Task cwt = task.ContinueWith(t =>
{
Console.WriteLine("任務(wù)完成後的結(jié)果是:{0}", t.Result.ToString());
});
task.Wait();
cwt.Wait();
Console.ReadLine();
Console.ReadKey();
}結(jié)果:

任務(wù)的串行
代碼:
static void Main(string[] args)
{
ConcurrentStack<int> stack = new ConcurrentStack<int>();
//t1先串行
var t1 = Task.Factory.StartNew(() =>
{
//入棧
stack.Push(1);
stack.Push(2);
});
//t2,t3并行執(zhí)行
var t2 = t1.ContinueWith(t =>
{
int result;
//出棧
stack.TryPop(out result);
Console.WriteLine("Task t2 result={0},Thread id {1}", result, Thread.CurrentThread.ManagedThreadId);
});
//t2,t3并行執(zhí)行
var t3 = t1.ContinueWith(t =>
{
int result;
//出棧
stack.TryPop(out result);
Console.WriteLine("Task t3 result={0},Thread id {1}", result, Thread.CurrentThread.ManagedThreadId);
});
//等待t2和t3執(zhí)行完
Task.WaitAll(t2, t3);
//t7串行執(zhí)行
var t4 = Task.Factory.StartNew(() =>
{
Console.WriteLine("當(dāng)前的集合數(shù)目:{0},Thread id {1}", stack.Count, Thread.CurrentThread.ManagedThreadId);
});
t4.Wait();
Console.ReadKey();
}結(jié)果:

子任務(wù)
代碼:
public static void Main()
{
Task<string[]> parent = new Task<string[]>(state =>
{
Console.WriteLine(state);
string[] result = new string[2];
//創(chuàng)建并啟動(dòng)子任務(wù)
new Task(() => { result[0] = "我是子任務(wù)1。"; }, TaskCreationOptions.AttachedToParent).Start();
new Task(() => { result[1] = "我是子任務(wù)2。"; }, TaskCreationOptions.AttachedToParent).Start();
return result;
}, "我是父任務(wù),並在處理過程中創(chuàng)建多個(gè)子任務(wù),所有的子任務(wù)完成以後我才會(huì)開始執(zhí)行。");
//任務(wù)處理完成后執(zhí)行的操作
parent.ContinueWith(t =>
{
Array.ForEach(t.Result, r => Console.WriteLine(r));
});
//啟動(dòng)父任務(wù)
parent.Start();
//等待任務(wù)結(jié)束 Wait只能等待父線程結(jié)束,沒辦法等到父線程的ContinueWith結(jié)束
//parent.Wait();
Console.ReadLine();
}結(jié)果:

動(dòng)態(tài)并行
代碼:
class Node
{
public Node Left { get; set; }
public Node Right { get; set; }
public string Text { get; set; }
}
class Program
{
static Node GetNode()
{
Node root = new Node
{
Left = new Node
{
Left = new Node
{
Text = "L-L"
},
Right = new Node
{
Text = "L-R"
},
Text = "L"
},
Right = new Node
{
Left = new Node
{
Text = "R-L"
},
Right = new Node
{
Text = "R-R"
},
Text = "R"
},
Text = "Root"
};
return root;
}
static void Main(string[] args)
{
Node root = GetNode();
DisplayTree(root);
}
static void DisplayTree(Node root)
{
var task = Task.Factory.StartNew(() => DisplayNode(root),
CancellationToken.None,
TaskCreationOptions.None,
TaskScheduler.Default);
task.Wait();
}
static void DisplayNode(Node current)
{
if (current.Left != null)
Task.Factory.StartNew(() => DisplayNode(current.Left),
CancellationToken.None,
TaskCreationOptions.AttachedToParent,
TaskScheduler.Default);
if (current.Right != null)
Task.Factory.StartNew(() => DisplayNode(current.Right),
CancellationToken.None,
TaskCreationOptions.AttachedToParent,
TaskScheduler.Default);
Console.WriteLine("當(dāng)前節(jié)點(diǎn)值:{0};處理的Thread ID ={1}", current.Text, Thread.CurrentThread.ManagedThreadId);
}
}結(jié)果:

到此這篇關(guān)于C#使用Task.ContinueWith組合任務(wù)的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#?WPF后臺(tái)動(dòng)態(tài)添加控件實(shí)戰(zhàn)教程
最近嘗試用wpf在后臺(tái)動(dòng)態(tài)添加控件,所以下面這篇文章主要給大家介紹了關(guān)于C#?WPF后臺(tái)動(dòng)態(tài)添加控件的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
C#實(shí)現(xiàn)流程圖設(shè)計(jì)器
這篇文章主要介紹了C#實(shí)現(xiàn)流程圖設(shè)計(jì)器,感興趣的小伙伴們可以參考一下2015-11-11
C#中AutoResetEvent控制線程用法小結(jié)
本文主要來自一道面試題,由于之前對(duì)AutoResetEvent的概念比較模糊,面試題題目很簡(jiǎn)潔:兩個(gè)線程交替打印0~100的奇偶數(shù),你可以先動(dòng)手試試,我主要是嘗試在一個(gè)方法里面完成這個(gè)任務(wù),需要的朋友可以參考下2022-07-07
Unity實(shí)現(xiàn)UI光暈效果(發(fā)光效果)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)UI光暈效果,發(fā)光效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01
C#實(shí)現(xiàn)日期格式轉(zhuǎn)換的公共方法類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)日期格式轉(zhuǎn)換的公共方法類,結(jié)合完整實(shí)例形式分析了C#針對(duì)各種常見日期格式的轉(zhuǎn)換方法,涉及C#字符串、日期、時(shí)間相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
c# dataTable 合并兩個(gè)列到一個(gè)新列中的簡(jiǎn)單實(shí)例
這篇文章介紹了c# dataTable 合并兩個(gè)列到一個(gè)新列中的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-10-10
C#中參數(shù)數(shù)組、引用參數(shù)和輸出參數(shù)示例詳解
這篇文章主要給大家介紹了關(guān)于C#中參數(shù)數(shù)組、引用參數(shù)和輸出參數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05

