Skip to content

Commit

Permalink
Parallel : Merge branch 'cs028' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
XuanThuLab committed Aug 21, 2020
2 parents 7805b45 + 87c7efb commit 9ce4dad
Show file tree
Hide file tree
Showing 17 changed files with 454 additions and 0 deletions.
27 changes: 27 additions & 0 deletions CS028_Parallel/For/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/For.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
42 changes: 42 additions & 0 deletions CS028_Parallel/For/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/For.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/For.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/For.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
8 changes: 8 additions & 0 deletions CS028_Parallel/For/For.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

</Project>
29 changes: 29 additions & 0 deletions CS028_Parallel/For/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Threading;
using System.Threading.Tasks;
namespace For
{
class Program
{
//In thông tin, Task ID và thread ID đang chạy
public static void PintInfo(string info) =>
Console.WriteLine($"{info, 10} task:{Task.CurrentId,3} " + $"thread: {Thread.CurrentThread.ManagedThreadId}");

public static void RunTask(int i) {
PintInfo($"Start {i,3}");
Task.Delay(1000).Wait(); // Task dừng 1s - rồi mới chạy tiếp
PintInfo($"Finish {i,3}");
}

public static void ParallelFor() {
ParallelLoopResult result = Parallel.For(1, 20, RunTask);
Console.WriteLine($"All task start and finish: {result.IsCompleted}");
}
static void Main(string[] args)
{
ParallelFor();
Console.WriteLine("Press any key ...");
Console.ReadKey();
}
}
}
27 changes: 27 additions & 0 deletions CS028_Parallel/ForAsync/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/ForAsync.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
42 changes: 42 additions & 0 deletions CS028_Parallel/ForAsync/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/ForAsync.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/ForAsync.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/ForAsync.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
8 changes: 8 additions & 0 deletions CS028_Parallel/ForAsync/ForAsync.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

</Project>
30 changes: 30 additions & 0 deletions CS028_Parallel/ForAsync/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ForAsync
{
class Program
{
//In thông tin, Task ID và thread ID đang chạy
public static void PintInfo(string info) =>
Console.WriteLine($"{info, 10} task:{Task.CurrentId,3} " + $"thread: {Thread.CurrentThread.ManagedThreadId}");

public static async void RunTask(int i) {
PintInfo($"Start {i,3}");
// Task.Delay(1000).Wait(); // Task dừng 1s - rồi mới chạy tiếp
await Task.Delay(1); // Task.Delay là một async nên có thể await, RunTask chuyển điểm gọi nó tại đây
PintInfo($"Finish {i,3}");
}

public static void ParallelFor() {
ParallelLoopResult result = Parallel.For(1, 20, RunTask);
Console.WriteLine($"All task started: {result.IsCompleted}");
}
static void Main(string[] args)
{
ParallelFor();
Console.WriteLine("Press any key ...");
Console.ReadKey();
}
}
}
27 changes: 27 additions & 0 deletions CS028_Parallel/ForEach/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/ForEach.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
42 changes: 42 additions & 0 deletions CS028_Parallel/ForEach/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/ForEach.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/ForEach.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/ForEach.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
8 changes: 8 additions & 0 deletions CS028_Parallel/ForEach/ForEach.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

</Project>
42 changes: 42 additions & 0 deletions CS028_Parallel/ForEach/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace ForEach
{
class Program
{
//In thông tin, Task ID và thread ID đang chạy
public static void PintInfo(string info) =>
Console.WriteLine($"{info, 10} task:{Task.CurrentId,3} " + $"thread: {Thread.CurrentThread.ManagedThreadId}");

public static async void RunTask(string s) {
PintInfo($"Start {s,10}");
await Task.Delay(1); // Task.Delay là một async nên có thể await, RunTask chuyển điểm gọi nó tại đây
PintInfo($"Finish {s,10}");
}

public static void ParallelFor() {

string[] source = new string[] {"xuanthulab1","xuanthulab2","xuanthulab3",
"xuanthulab4","xuanthulab5","xuanthulab6",
"xuanthulab7","xuanthulab8","xuanthulab9"};
// Dùng List thì khởi tạo
// List<string> source = new List<string>();
// source.Add("xuanthulab1");


ParallelLoopResult result = Parallel.ForEach(
source, RunTask
);

Console.WriteLine($"All task started: {result.IsCompleted}");
}
static void Main(string[] args)
{
ParallelFor();
Console.WriteLine("Press any key ...");
Console.ReadKey();
}
}
}
27 changes: 27 additions & 0 deletions CS028_Parallel/Invoke/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.1/Invoke.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
Loading

0 comments on commit 9ce4dad

Please sign in to comment.