本文整理汇总了C#中TaskCompletionSource.TrySetFromTask方法的典型用法代码示例。如果您正苦于以下问题:C# TaskCompletionSource.TrySetFromTask方法的具体用法?C# TaskCompletionSource.TrySetFromTask怎么用?C# TaskCompletionSource.TrySetFromTask使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TaskCompletionSource
的用法示例。
在下文中一共展示了TaskCompletionSource.TrySetFromTask方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: object
public void TrySetFromTask_IfSourceTaskIsSuccessfulAndOfDifferentResultType_SucceedsTaskCompletionSourceAndSetsDefaultValueAsResult()
{
TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
Task<object> successfulTask = TaskHelpers.FromResult(new object());
tcs.TrySetFromTask(successfulTask);
Assert.Equal(TaskStatus.RanToCompletion, tcs.Task.Status);
Assert.Equal(null, tcs.Task.Result);
}
示例2: TrySetFromTask_IfSourceTaskIsSuccessfulAndOfSameResultType_SucceedsTaskCompletionSourceAndSetsResult
public void TrySetFromTask_IfSourceTaskIsSuccessfulAndOfSameResultType_SucceedsTaskCompletionSourceAndSetsResult()
{
TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
Task<string> successfulTask = TaskHelpers.FromResult("abc");
tcs.TrySetFromTask(successfulTask);
Assert.Equal(TaskStatus.RanToCompletion, tcs.Task.Status);
Assert.Equal("abc", tcs.Task.Result);
}
示例3: TrySetFromTask_IfSourceTaskIsFaulted_FaultsTaskCompletionSource
public void TrySetFromTask_IfSourceTaskIsFaulted_FaultsTaskCompletionSource()
{
TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
Exception exception = new Exception();
Task faultedTask = TaskHelpers.FromError<object>(exception);
tcs.TrySetFromTask(faultedTask);
Assert.Equal(TaskStatus.Faulted, tcs.Task.Status);
Assert.Same(exception, tcs.Task.Exception.InnerException);
}
示例4: TrySetFromTask_IfSourceTaskIsCanceled_CancelsTaskCompletionSource
public void TrySetFromTask_IfSourceTaskIsCanceled_CancelsTaskCompletionSource()
{
TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
Task canceledTask = TaskHelpers.Canceled<object>();
tcs.TrySetFromTask(canceledTask);
Assert.Equal(TaskStatus.Canceled, tcs.Task.Status);
}
示例5: TryShowAsync
public INavigationOperation TryShowAsync(IViewModel viewModel, IDataContext context,
IViewModelPresenter parentPresenter)
{
if (!CanShowViewModel(viewModel, context, parentPresenter))
return null;
var tcs = new TaskCompletionSource<object>();
var operation = new NavigationOperation(tcs.Task);
context = context.ToNonReadOnly();
context.AddOrUpdate(NavigationConstants.ViewModel, viewModel);
var provider = viewModel.GetIocContainer(true).Get<INavigationProvider>();
provider.CurrentNavigationTask.TryExecuteSynchronously(_ =>
{
try
{
var task = provider.NavigateAsync(operation.ToOperationCallback(), context);
tcs.TrySetFromTask(task);
}
catch (Exception e)
{
tcs.TrySetException(e);
throw;
}
});
return operation;
}
示例6: Show
private void Show(IWindowViewMediator viewMediator, INavigationOperation operation, IDataContext context, TaskCompletionSource<object> tcs)
{
try
{
var task = viewMediator.ShowAsync(operation.ToOperationCallback(), context);
_currentTask = task;
tcs.TrySetFromTask(task);
}
catch (Exception e)
{
tcs.TrySetException(e);
throw;
}
}