.NET 4.5ããã¿ã¹ã¯ã®éå§æ¹æ³ã«æ°ããã¡ã½ãããç¨æããã¾ããã
Task.Run(...) // Actionã¨ãFuncã¨ãCancellationTokenã¨ã
- Task.Run ã¡ã½ãã
.NET 4.0ã®ã¨ãã¯
Task.Factory.StartNew(...)
ãå©ç¨ãã¦ã¿ã¹ã¯ãæ§ç¯ããã·ã§ã¼ãã«ãããããã¾ããã
ããã«å ãã¦ãTask.Runã®å½¢å¼ã追å ããã¾ããã
å人çã«ã¯ãTask.Runã®å½¢å¼ã®æ¹ãåãããããã¦å¥½ãã§ãã
以ä¸ããµã³ãã«ã§ãã
namespace Sazare.Samples { using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Sazare.Common; /// <summary> /// ã¿ã¹ã¯ä¸¦åã©ã¤ãã©ãªã«ã¤ãã¦ã®ãµã³ãã«ã§ãã /// </summary> /// <remarks> /// ã¿ã¹ã¯ä¸¦åã©ã¤ãã©ãªã¯ã.NET 4.0ãã追å ãããã©ã¤ãã©ãªã§ãã /// Task.Runã¡ã½ããã¯ã.NET 4.5ãã追å ãããã¡ã½ããã§ãã /// </remarks> [Sample] class TaskSamples05 : IExecutable { public void Execute() { // // .NET 4.5ããã¿ã¹ã¯ã®éå§æ¹æ³ã« // Task.Run // ã¡ã½ããã追å ãããã // 以åã®Task.Factory.StartNewã¡ã½ãã㨠// åæ§ã«å©ç¨ããäºãã§ããã // // http://msdn.microsoft.com/ja-jp/library/system.threading.tasks.task.run(v=vs.110).aspx // // // å¼æ°ã«Actionãæå®ï¼æãã·ã³ãã«ãªãã¿ã¼ã³) // Task.Run(() => Output.WriteLine("Task.Run(Action)")).Wait(); // // å¼æ°ã«Funcãæå® // var task1 = Task.Run(() => 100); Output.WriteLine(task1.Result); // // C# 5.0ã®async/awaitãå©ç¨. // ãµã³ãã«ã®é½åä¸RunAsyncã®å¼ã³åºãã¯awaitãã¦ããªã // RunAsync(); // // ä¸ã®è¨è¿°ã®Awaiterç // ãµã³ãã«ã®é½åä¸ããã¨task2ã®å®äºå¾ ã¡ããã¦ãã // var task2 = Task.Run(() => 300); var awaiter = task2.GetAwaiter(); awaiter.OnCompleted(() => { Output.WriteLine(awaiter.GetResult()); } ); task2.Wait(); // // Task.Runã¡ã½ããã«ã¯Action, Funcãæå®ãã¦ãä¸ã¤ãCancellationTokenã // æå®ãããã¨ãã§ããã // var tokenSource = new CancellationTokenSource(); var token = tokenSource.Token; var printDot = new Action(() => { while (true) { if (token.IsCancellationRequested) { Output.WriteLine(string.Empty); break; } Output.Write("."); Thread.Sleep(TimeSpan.FromMilliseconds(500)); } } ); var task3 = Task.Run(printDot, token); Thread.Sleep(TimeSpan.FromSeconds(3)); tokenSource.Cancel(); task3.Wait(); tokenSource.Dispose(); } async Task RunAsync() { int result = await Task.Run(() => 200); Output.WriteLine(result); } } }
å®è¡çµæã¯ã以ä¸ã®ããã«ãªãã¾ãã
================== START ================== Task.Run(Action) 100 200 300 ...... ================== END ==================
ã¢ãããã以åã®è¨äºã¯ä»¥ä¸ãããåç §ãã ããã¾ãã
- ã¿ã¹ã¯ä¸¦åã©ã¤ãã©ãª(TPL)ã«ã¤ãã¦ã®ãªã½ã¼ã¹ (Task Parallel Library, Parallel.For, Thread, ThreadPool, éåæ, 並å)
- ã¿ã¹ã¯ä¸¦åã©ã¤ãã©ãªå ¥éè¨-000 (äºå§ã, æ®´ãæ¸ãã¡ã¢)
- ã¿ã¹ã¯ä¸¦åã©ã¤ãã©ãªå ¥éè¨-001 (ã¿ã¹ã¯ã®å®è¡, TPL, Task.Factory.StartNew, Wait, WaitAll)
- ã¿ã¹ã¯ä¸¦åã©ã¤ãã©ãªå ¥éè¨-002 (ã¿ã¹ã¯ã®å®è¡, TPL, Task.ctor, Wait, WaitAll)
- ã¿ã¹ã¯ä¸¦åã©ã¤ãã©ãªå ¥éè¨-003 (ã¿ã¹ã¯ã®å ¥ãå, ãã¿ãããããå ¥ãåã®ã¿ã¹ã¯, 親ã®ã¿ã¹ã¯ã¨ã®é¢é£ãæããªã, TaskCreationOptions.None)
- ã¿ã¹ã¯ä¸¦åã©ã¤ãã©ãªå ¥éè¨-004 (ã¿ã¹ã¯ã®å ¥ãå, 親ã®ã¿ã¹ã¯ã«ã¢ã¿ãããããå ¥ãåã¿ã¹ã¯, 親ã®ã¿ã¹ã¯ã¨ã®é¢é£ãæã¤, TaskCreationOptions.AttachedToParent)
éå»ã®è¨äºã«ã¤ãã¦ã¯ã以ä¸ã®ãã¼ã¸ãããåç §ä¸ããã
- ããããåå¿é²æ¥è¨ã¾ã¨ã
ãµã³ãã«ã³ã¼ãã¯ã以ä¸ã®å ´æã§å ¬éãã¦ãã¾ãã
- ããããåå¿é²æ¥è¨ãµã³ãã«ã½ã¼ã¹ç½®ãå ´