Concurrency and Source Panels
Task Method Group
Start task work with named methods so each worker body remains traceable.
Task Method Group
TaskMethodGroup.cs
using System;
using System.Threading.Tasks;
class Program
{
static int baseValue;
static int CalculateLeft()
{
int result = baseValue + 3;
return result;
}
static int CalculateRight()
{
int result = baseValue * 2;
return result;
}
static void Main()
{
int seed = ;
baseValue = seed;
Task<int> left = Task.Run(CalculateLeft);
Task<int> right = Task.Run(CalculateRight);
int total = left.Result + right.Result;
Console.WriteLine($"seed={seed}");
Console.WriteLine($"total={total}");
}
}
using System;
using System.Threading.Tasks;
class Program
{
static int baseValue;
static int CalculateLeft()
{
int result = baseValue + 3;
return result;
}
static int CalculateRight()
{
int result = baseValue * 2;
return result;
}
static void Main()
{
int seed = ;
baseValue = seed;
Task<int> left = Task.Run(CalculateLeft);
Task<int> right = Task.Run(CalculateRight);
int total = left.Result + right.Result;
Console.WriteLine($"seed={seed}");
Console.WriteLine($"total={total}");
}
}
using System;
using System.Threading.Tasks;
class Program
{
static int baseValue;
static int CalculateLeft()
{
int result = baseValue + 3;
return result;
}
static int CalculateRight()
{
int result = baseValue * 2;
return result;
}
static void Main()
{
int seed = ;
baseValue = seed;
Task<int> left = Task.Run(CalculateLeft);
Task<int> right = Task.Run(CalculateRight);
int total = left.Result + right.Result;
Console.WriteLine($"seed={seed}");
Console.WriteLine($"total={total}");
}
}
task
`Task.Run` can receive a method group, keeping the concurrent worker logic in ordinary named methods.