本文整理汇总了C#中Chain.ExecuteAsync方法的典型用法代码示例。如果您正苦于以下问题:C# Chain.ExecuteAsync方法的具体用法?C# Chain.ExecuteAsync怎么用?C# Chain.ExecuteAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Chain
的用法示例。
在下文中一共展示了Chain.ExecuteAsync方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
public static void Run()
{
Chain<int, double> chain = new Chain<int, double>()
.Link<int>(x =>
{
// Generate random array of ints
int[] nums = new int[x];
Random rand = new Random();
for (int i = 0; i < x; i++)
{
nums[i] = rand.Next(100);
}
return nums;
})
.Link<int[]>(x =>
{
// order the numbers (pointless but why not)
return x.OrderBy(y => y).ToArray();
})
.Link<int[]>(x =>
{
// compute the average and return it
double average = 0.0;
for (int i = 0; i < x.Length; i++)
{
average += x[i];
}
return average / x.Length;
});
int count = 0;
int started = 0;
chain.ExecuteFinished += (sender, e) =>
{
Interlocked.Increment(ref count);
};
int iters = 1000000;
Queue<int> diff = new Queue<int>(iters);
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < iters; i++)
{
started++;
chain.ExecuteAsync(10);
diff.Enqueue(started - count);
}
sw.Stop();
Console.WriteLine("Took: " + sw.Elapsed.ToString());
}
示例2: TestAsyncExecution
public void TestAsyncExecution()
{
Chain<int, int> chain = new Chain<int, int>()
.Link<int>(x => x + 1)
.Link<int>(x => x + 1)
.Link<int>(x => x + 1)
.Link<int>(x => x + 1);
int output = -1;
chain.ExecuteFinished += (sender, e) => output = e.Output;
chain.ExecuteAsync(0);
SpinWait.SpinUntil(() => output != -1, 250);
Assert.AreEqual<int>(4, output);
}
示例3: Run
public static void Run()
{
Chain<int, double> chain = new Chain<int, double>()
.Link<int>(x =>
{
// Generate random array of ints
int[] nums = new int[x];
Random rand = new Random();
for (int i = 0; i < x; i++)
{
nums[i] = rand.Next(100);
}
return nums;
})
.Link<int[]>(x =>
{
// order the numbers (pointless but why not)
return x.OrderBy(y => y).ToArray();
})
.Link<int[]>(x =>
{
// compute the average and return it
double average = 0.0;
for (int i = 0; i < x.Length; i++)
{
average += x[i];
}
return average / x.Length;
});
chain.ExecuteFinished += (sender, e) =>
{
Console.WriteLine("Async execute finished, result: " + e.Output);
};
// Start execution of our chain, passing in a large initial argument so it takes some time
chain.ExecuteAsync(10000);
}
示例4: TestAsyncExecutionError
public void TestAsyncExecutionError()
{
Chain<int, int> chain = new Chain<int, int>()
.Link<int>(x => x + 1)
.Link<int>(x =>
{
if (x == 1)
throw new Exception(x.ToString());
else
return x + 1;
})
.Link<int>(x => x + 1)
.Link<int>(x => x + 1);
Exception ex = null;
chain.ExecuteFinished += (sender, e) =>
{
ex = e.Exception;
};
chain.ExecuteAsync(0);
SpinWait.SpinUntil(() => ex != null, 250);
Assert.IsNotNull(ex);
Assert.AreEqual<bool>(true, ex is ChainExecutionException);
}
示例5: TestAsyncTypeError
public void TestAsyncTypeError()
{
Chain<int, int> chain = new Chain<int, int>()
.Link<int>(x => x + 1)
.Link<int>(x => x + "1")
.Link<int>(x => x + 1)
.Link<int>(x => x + 1);
Exception ex = null;
chain.ExecuteFinished += (sender, e) =>
{
ex = e.Exception;
};
chain.ExecuteAsync(0);
SpinWait.SpinUntil(() => ex != null, 250);
Assert.IsNotNull(ex);
Assert.AreEqual<bool>(true, ex is LinkArgumentException);
}