Last active
June 7, 2021 10:29
-
-
Save MeinLiX/d618d3cc41e721e7c6a6f86486b64720 to your computer and use it in GitHub Desktop.
Parallel fn and operation ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace Project | |
{ | |
class Program | |
{ | |
private struct FnProp | |
{ | |
public FnProp(int time, string fnName, bool result, CancellationToken ct) | |
{ | |
Time = time; | |
FnName = fnName; | |
Result = result; | |
CT = ct; | |
} | |
public readonly int Time { get; } | |
public readonly string FnName { get; } | |
public readonly bool Result { get; } | |
public CancellationToken CT { get; } | |
} | |
private async Task<bool> Fn(FnProp props) | |
{ | |
Console.WriteLine($"Fn {props.FnName} is Started;"); | |
await Task.Delay(props.Time, props.CT); | |
Console.WriteLine($"Fn {props.FnName} is Over;"); | |
return props.Result; | |
} | |
private async Task<bool> FirstOrSecond(FnProp F, FnProp G) | |
{ | |
var fn_F = Fn(F); | |
var fn_G = Fn(G); | |
var fn_F_res = await fn_F; | |
Console.WriteLine($"{fn_F.GetType().Name} returned {fn_F_res}"); | |
if (fn_F_res) { | |
Console.WriteLine($"don't expect the function {fn_G.GetType().Name}."); | |
return fn_F_res; | |
} | |
else | |
Console.WriteLine($"expect the function {fn_G.GetType().Name}."); | |
var fn_G_res = await fn_G; | |
Console.WriteLine($"{fn_G_res.GetType().Name} returned {fn_G_res}"); | |
return fn_G_res; | |
} | |
static void Main(string[] args) => new Program().Run().Wait(); | |
public async Task Run() | |
{ | |
Console.WriteLine($"1 (T||T) = {await FirstOrSecond(new(2000, "F", true, CancellationToken.None), new(4000, "G", true,CancellationToken.None))}\n\n"); | |
Console.WriteLine($"2 (T||F) = {await FirstOrSecond(new(2000, "F", true, CancellationToken.None), new(4000, "G", false, CancellationToken.None))}\n\n"); | |
Console.WriteLine($"3 (F||T) = {await FirstOrSecond(new(2000, "F", false, CancellationToken.None), new(4000, "G", true, CancellationToken.None))}\n\n"); | |
Console.WriteLine($"4 (F||F) = {await FirstOrSecond(new(2000, "F", false, CancellationToken.None), new(4000, "G", false, CancellationToken.None))}\n\n"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment