當前位置: 首頁>>代碼示例>>C#>>正文


C# TaskCompletionSource.TrySetResult方法代碼示例

本文整理匯總了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;
        }
開發者ID:pglazkov,項目名稱:Linqua,代碼行數:28,代碼來源:DispatcherServiceExtensions.cs

示例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);
         }
     };
 }
開發者ID:Lakerfield,項目名稱:AsyncEx,代碼行數:19,代碼來源:AsyncFactory.cs

示例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;
        }
開發者ID:andrekiba,項目名稱:KLabAsyncAwait,代碼行數:24,代碼來源:APM_EAP.cs

示例4: TrySetResult_CompletesTask

 public void TrySetResult_CompletesTask()
 {
     Test.Async(async () =>
     {
         var tcs = new TaskCompletionSource();
         tcs.TrySetResult();
         await tcs.Task;
     });
 }
開發者ID:Lakerfield,項目名稱:AsyncEx,代碼行數:9,代碼來源:TaskCompletionSourceUnitTests.cs

示例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;
		}
開發者ID:fadafido,項目名稱:tojeero,代碼行數:37,代碼來源:FacebookService.cs

示例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));
            }
        }
開發者ID:wasabii,項目名稱:UcmaKit,代碼行數:55,代碼來源:VoiceXmlApplication.cs


注:本文中的Nito.AsyncEx.TaskCompletionSource.TrySetResult方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。