c#并行編程示例分享
ParallelTest.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace ParallelTest
{
class ParallelTest
{
private static int Timed_Message(String arg_Message, int arg_Interval)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Source {0} - Cycle {1} for Interval {2}", arg_Message, i, arg_Interval);
Thread.Sleep(1000 * arg_Interval);
}
Console.WriteLine("{0} - Complete", arg_Message);
return 0;
}
static void Main(string[] args)
{
int RetCode = 0;
Task RedistributionTask = new Task(() => RetCode = Timed_Message("Five ", 4));
RedistributionTask.Start();
Task AltRedistributionTask = new Task(() => RetCode = Timed_Message("Three ", 2));
AltRedistributionTask.Start();
//Timed_Message("Main", 6);
// wait for input before exiting
Console.WriteLine("Press enter to finish after both [Complete] messages appear.");
Console.ReadLine();
}
}
}
輸出結(jié)果
Press enter to finish after both [Complete] messages appear.
Source Five - Cycle 0 for Interval 4
Source Three - Cycle 0 for Interval 2
Source Three - Cycle 1 for Interval 2
Source Five - Cycle 1 for Interval 4
Source Three - Cycle 2 for Interval 2
Source Three - Cycle 3 for Interval 2
Source Five - Cycle 2 for Interval 4
Source Three - Cycle 4 for Interval 2
Source Three - Cycle 5 for Interval 2
Source Five - Cycle 3 for Interval 4
Source Three - Cycle 6 for Interval 2
Source Three - Cycle 7 for Interval 2
Source Five - Cycle 4 for Interval 4
Source Three - Cycle 8 for Interval 2
Source Three - Cycle 9 for Interval 2
Source Five - Cycle 5 for Interval 4
Three - Complete
Source Five - Cycle 6 for Interval 4
Source Five - Cycle 7 for Interval 4
Source Five - Cycle 8 for Interval 4
Source Five - Cycle 9 for Interval 4
Five - Complete
相關(guān)文章
分析C# Dictionary的實(shí)現(xiàn)原理
對(duì)于C#中的Dictionary類相信大家都不陌生,這是一個(gè)Collection(集合)類型,可以通過Key/Value(鍵值對(duì)的形式來存放數(shù)據(jù);該類最大的優(yōu)點(diǎn)就是它查找元素的時(shí)間復(fù)雜度接近O(1)。那么什么樣的設(shè)計(jì)能使得Dictionary類實(shí)現(xiàn)O(1)的時(shí)間復(fù)雜度呢2021-06-06字符串替換Replace僅替換第一個(gè)字符串匹配項(xiàng)
C#里面的String.Replace(string,string)方法替換的時(shí)候是替換所有的匹配項(xiàng),我們需要只替換第一個(gè)匹配項(xiàng),寫一個(gè)方法來實(shí)現(xiàn)這個(gè)功能2013-12-12C#開發(fā)Windows UWP系列之對(duì)話框MessageDialog和ContentDialog
這篇文章介紹了C#開發(fā)Windows UWP系列之對(duì)話框MessageDialog和ContentDialog,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06C#設(shè)計(jì)模式編程中運(yùn)用適配器模式結(jié)構(gòu)實(shí)戰(zhàn)演練
這篇文章主要介紹了C#設(shè)計(jì)模式編程中運(yùn)用適配器模式結(jié)構(gòu)實(shí)戰(zhàn)演練,并總結(jié)了適配器模式的優(yōu)缺點(diǎn)和適用場(chǎng)景以及.NET框架中的應(yīng)用,需要的朋友可以參考下2016-02-02