本文整理汇总了C#中Nito.AsyncEx.TaskCompletionSource.TrySetResult方法的典型用法代码示例。如果您正苦于以下问题:C# TaskCompletionSource.TrySetResult方法的具体用法?C# TaskCompletionSource.TrySetResult怎么用?C# TaskCompletionSource.TrySetResult使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Nito.AsyncEx.TaskCompletionSource
的用法示例。
在下文中一共展示了TaskCompletionSource.TrySetResult方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InvokeAsync
public static Task InvokeAsync([NotNull] this IDispatcherService service, [NotNull] Func<Task> action)
{
Guard.NotNull(service, nameof(service));
Guard.NotNull(action, nameof(action));
var tcs = new TaskCompletionSource();
service.InvokeAsync(new Action(() =>
{
action().ContinueWith(t =>
{
if (t.Exception != null)
{
tcs.TrySetException(t.Exception);
return;
}
if (t.IsCanceled)
{
tcs.TrySetCanceled();
}
tcs.TrySetResult();
});
}));
return tcs.Task;
}
示例2: Callback
private static AsyncCallback Callback(Action<IAsyncResult> endMethod, TaskCompletionSource<object> tcs)
{
return asyncResult =>
{
try
{
endMethod(asyncResult);
tcs.TrySetResult(null);
}
catch (OperationCanceledException)
{
tcs.TrySetCanceled();
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
};
}
示例3: DumpWebPageAsync
private static Task<string> DumpWebPageAsync(WebClient client, Uri uri)
{
//si utilizza TaskCompletionSource che gestisce un task figlio
var tcs = new TaskCompletionSource<string>();
DownloadStringCompletedEventHandler handler = null;
handler = (sender, args) =>
{
client.DownloadStringCompleted -= handler;
if (args.Cancelled)
tcs.TrySetCanceled();
else if (args.Error != null)
tcs.TrySetException(args.Error);
else
tcs.TrySetResult(args.Result);
};
client.DownloadStringCompleted += handler;
client.DownloadStringAsync(uri);
return tcs.Task;
}
示例4: TrySetResult_CompletesTask
public void TrySetResult_CompletesTask()
{
Test.Async(async () =>
{
var tcs = new TaskCompletionSource();
tcs.TrySetResult();
await tcs.Task;
});
}
示例5: getUserData
private Task<FacebookUser> getUserData(Facebook.CoreKit.AccessToken token)
{
var fbUser = new FacebookUser()
{
Token = token.TokenString,
ExpiryDate = token.ExpirationDate.ToDateTime()
};
var completion = new TaskCompletionSource<FacebookUser>();
var request = new GraphRequest("me", NSDictionary.FromObjectAndKey(new NSString("id, first_name, last_name, email, location"), new NSString("fields")), "GET");
request.Start((connection, result, error) =>
{
if(error != null)
{
Mvx.Trace(MvxTraceLevel.Error, error.ToString());
}
else
{
var data = NSJsonSerialization.Serialize(result as NSDictionary, 0, out error);
if(error != null)
{
Mvx.Trace(MvxTraceLevel.Error, error.ToString());
}
else
{
var json = new NSString(data, NSStringEncoding.UTF8).ToString();
fbUser.User = JsonConvert.DeserializeObject<User>(json);
fbUser.User.ProfilePictureUrl = string.Format("https://graph.facebook.com/{0}/picture?width={1}&height={1}", fbUser.User.ID, Constants.FBProfilePicSize);
}
completion.TrySetResult(fbUser);
}
});
return completion.Task;
}
示例6: HandleAudioVideoCall
protected override async Task HandleAudioVideoCall(CallReceivedEventArgs<AudioVideoCall> args, CancellationToken cancellationToken)
{
try
{
// only new conversations supported
if (!args.IsNewConversation)
return;
// locate destination application URI
var uri = applications.GetOrDefault(new RealTimeAddress(args.Call.Conversation.LocalParticipant.Uri));
if (uri == null)
throw new Exception(string.Format("No configured Application for {0}.", args.Call.Conversation.LocalParticipant.Uri));
// signals activation of flow
var active = new TaskCompletionSource();
// subscribe to flow configuration requested
args.Call.AudioVideoFlowConfigurationRequested += (s, a) =>
{
// subscribe to flow state changes
if (a.Flow != null)
a.Flow.StateChanged += (s2, a2) =>
{
if (a2.PreviousState != MediaFlowState.Active &&
a2.State == MediaFlowState.Active)
Dispatch(() =>
{
active.TrySetResult();
});
};
};
// accept the call and wait for an active flow
await args.Call.AcceptAsync();
await active.Task;
// initiate browser
using (var browser = new Browser())
{
var result = new TaskCompletionSource<VoiceXmlResult>();
browser.SessionCompleted += (s3, a3) => OnSessionCompleted(result, a3);
browser.Disconnecting += (s3, a3) => OnDisconnecting(result, a3);
browser.Disconnected += (s3, a3) => OnDisconnected(result, a3);
browser.Transferring += (s3, a3) => OnTransferring(result, a3);
browser.Transferred += (s3, a3) => OnTransfered(result, a3);
browser.SetAudioVideoCall(args.Call);
browser.RunAsync(uri, null);
await result.Task;
}
}
catch (Exception e)
{
OnUnhandledException(new ExceptionEventArgs(e, ExceptionSeverityLevel.Error));
}
}