本文整理匯總了C#中System.Threading.Tasks.TaskCompletionSource<TResult>類的典型用法代碼示例。如果您正苦於以下問題:C# TaskCompletionSource<TResult>類的具體用法?C# TaskCompletionSource<TResult>怎麽用?C# TaskCompletionSource<TResult>使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
TaskCompletionSource<TResult>類屬於System.Threading.Tasks命名空間,在下文中一共展示了TaskCompletionSource<TResult>類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
class TCSDemo
{
// Demonstrated features:
// TaskCompletionSource ctor()
// TaskCompletionSource.SetResult()
// TaskCompletionSource.SetException()
// Task.Result
// Expected results:
// The attempt to get t1.Result blocks for ~1000ms until tcs1 gets signaled. 15 is printed out.
// The attempt to get t2.Result blocks for ~1000ms until tcs2 gets signaled. An exception is printed out.
static void Main()
{
TaskCompletionSource<int> tcs1 = new TaskCompletionSource<int>();
Task<int> t1 = tcs1.Task;
// Start a background task that will complete tcs1.Task
Task.Factory.StartNew(() =>
{
Thread.Sleep(1000);
tcs1.SetResult(15);
});
// The attempt to get the result of t1 blocks the current thread until the completion source gets signaled.
// It should be a wait of ~1000 ms.
Stopwatch sw = Stopwatch.StartNew();
int result = t1.Result;
sw.Stop();
Console.WriteLine("(ElapsedTime={0}): t1.Result={1} (expected 15) ", sw.ElapsedMilliseconds, result);
// ------------------------------------------------------------------
// Alternatively, an exception can be manually set on a TaskCompletionSource.Task
TaskCompletionSource<int> tcs2 = new TaskCompletionSource<int>();
Task<int> t2 = tcs2.Task;
// Start a background Task that will complete tcs2.Task with an exception
Task.Factory.StartNew(() =>
{
Thread.Sleep(1000);
tcs2.SetException(new InvalidOperationException("SIMULATED EXCEPTION"));
});
// The attempt to get the result of t2 blocks the current thread until the completion source gets signaled with either a result or an exception.
// In either case it should be a wait of ~1000 ms.
sw = Stopwatch.StartNew();
try
{
result = t2.Result;
Console.WriteLine("t2.Result succeeded. THIS WAS NOT EXPECTED.");
}
catch (AggregateException e)
{
Console.Write("(ElapsedTime={0}): ", sw.ElapsedMilliseconds);
Console.WriteLine("The following exceptions have been thrown by t2.Result: (THIS WAS EXPECTED)");
for (int j = 0; j < e.InnerExceptions.Count; j++)
{
Console.WriteLine("\n-------------------------------------------------\n{0}", e.InnerExceptions[j].ToString());
}
}
}
}