C# - Task, Action, Func

参考サイト

so-zou.jp

genesis-tech.jp

genesis-tech.jp

work-note32.com

sasaki816.hatenablog.com

Action

// デリゲートを使用1
Action a1 = new Action(delegate () { Console.WriteLine("action a1"); });
a1();

// デリゲートを使用2
Action a2 = delegate () { Console.WriteLine("action a2"); };
a2();

// ラムダ式Action
Action a3 = new Action(() => { Console.WriteLine("action a3"); });
a3();

// ラムダ式Action
Action a4 = () => { Console.WriteLine("action - ramda - a4"); };
a4();

// 引数が一つあり、
Action<int, string> a5 = (int v1, string s1) => { Console.WriteLine("action - ramda - a5 - p:{0},{1}", v1, s1); };
a5(100, "hello");

// varを使用、引数が複数あり
var a6 = (int v1, int v2) => { Console.WriteLine("action - ramda - a6 - p:{0},{1}", v1, v2); };
a6(100, 200);

//---------- 結果
// action a1
// action a2
// action a3
// action - ramda - a4
// action - ramda - a5 - p:100,hello
// action - ramda - a6 - p:100,200

Func<T,TResult>

// 引数:string,戻値:bool
Func<string, bool> f1 = (string name) => { 
    return string.IsNullOrEmpty(name);
};
Console.WriteLine("Function - f1:" + f1("aoi")); 

// 引数:string,string, 戻値:string
Func<string, string, string> f2 = (string type, string name) => {
    return $"{type}-{name}";
};
Console.WriteLine("Function - f2:" + f2("attaker", "aoi"));

// varを使用、引数:string, 戻値:bool
var f3 = (string type) => {
    return string.IsNullOrEmpty(type);
};
Console.WriteLine("Function - f3:" + f3("aoi"));
Console.WriteLine("Function - f3:" + f3(""));

//---------- 結果
// Function - f1:False
// Function - f2:attaker-aoi
// Function - f3:False
// Function - f3:True
// Execute4 - start

Task

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Console1
{
    public class MyTask
    {
        public MyTask()
        {
                
        }

        public void Execute1()
        {
            Console.WriteLine("Execute1 - start");

            //---- Task.Run引数なし
            Task.Run(() => {
                Console.WriteLine("task - start");
                Thread.Sleep(2000);
                Console.WriteLine("task - end");
            });

            Console.WriteLine("Execute1 - end");
        }


        public void Execute2()
        {
            Console.WriteLine("Execute2 - start");

            //---- Task.Run引数あり
            //Task.Run(() => { DoHeayWork("aoi"); });
            //Task.Run(() => { DoHeayWork("kii"); });

            //---- Task.Run引数あり、wait all
            var t1 = Task.Run(() => { DoHeayWork("aoi"); });
            var t2 = Task.Run(() => { DoHeayWork("kii"); });
            Task.WaitAll(t1, t2);

            Console.WriteLine("Execute2 - all finish.");
            Console.WriteLine("Execute2 - end");
        }

        public void DoHeayWork(string name)
        {
            Console.WriteLine($"{name} DoHeayWork - start");
            Thread.Sleep(1000);
            Console.WriteLine($"{name} is woking.");
            Thread.Sleep(1000);
            Console.WriteLine($"{name} DoHeayWork - end");
        }

        public void Execute3()
        {
            Console.WriteLine($"Execute3 - start");

            var t1 = Task.Run(() => {
                Console.WriteLine("hevy work - start");
                Thread.Sleep(2000);
                Console.WriteLine("hevy work - end");
                return true;
            });

            Console.WriteLine($"Execute3 - Result:{t1.Result}");     // 同期
            Console.WriteLine($"Execute3 - finish");
        }

        public void Execute4()
        {
            Console.WriteLine($"Execute4 - start");

            var tasks = new List<Task<Tuple<int, bool>>>();

            for (int i = 0; i < 3; i++) {
                var index = i;
                var task = Task <Tuple<int, bool>>.Run(() => TaskProc(index));
                tasks.Add(task);
            }

            Task.WaitAll([.. tasks]);

            foreach (var t1 in tasks)
            {
                Console.WriteLine($"task.Result: {t1.Result.Item1},{t1.Result.Item2 }");
            }

            Console.WriteLine($"Execute4 - finish");
        }


        public Tuple<int, bool> TaskProc(int index) {
            Console.WriteLine($"hevy work({index}) - start");
            Thread.Sleep(2000);
            Console.WriteLine($"$hevy work({index}) - end");
            return new Tuple<int, bool>(index, true) ;
        }
    }
}

// ---------- 結果
// Execute4 - start
// $hevy work(0) - end
// $hevy work(2) - end
// $hevy work(1) - end
// task.Result: 0,True
// task.Result: 1,True
// task.Result: 2,True
// Execute4 - finish