当前位置: 首页>>代码示例>>C#>>正文


C# TaskCompletionSource.TrySetException方法代码示例

本文整理汇总了C#中Nito.AsyncEx.TaskCompletionSource.TrySetException方法的典型用法代码示例。如果您正苦于以下问题:C# TaskCompletionSource.TrySetException方法的具体用法?C# TaskCompletionSource.TrySetException怎么用?C# TaskCompletionSource.TrySetException使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Nito.AsyncEx.TaskCompletionSource的用法示例。


在下文中一共展示了TaskCompletionSource.TrySetException方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: TrySetException_FaultsTask

 public void TrySetException_FaultsTask()
 {
     var e = new InvalidOperationException();
     var tcs = new TaskCompletionSource();
     tcs.TrySetException(e);
     Assert.IsTrue(tcs.Task.IsFaulted);
     Assert.AreSame(e, tcs.Task.Exception.InnerException);
 }
开发者ID:Lakerfield,项目名称:AsyncEx,代码行数:8,代码来源:TaskCompletionSourceUnitTests.cs

示例3: TryCompleteFromCompletedTaskTResult_PropagatesException

        public void TryCompleteFromCompletedTaskTResult_PropagatesException()
        {
            AsyncContext.Run(async () =>
            {
                var source = new TaskCompletionSource<int>();
                source.TrySetException(new NotImplementedException());

                var tcs = new TaskCompletionSource<int>();
                tcs.TryCompleteFromCompletedTask(source.Task);
                await AssertEx.ThrowsExceptionAsync<NotImplementedException>(() => tcs.Task);
            });
        }
开发者ID:Lakerfield,项目名称:AsyncEx,代码行数:12,代码来源:TaskCompletionSourceExtensionsUnitTests.cs

示例4: 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

示例5: SpeakAsync

        /// <summary>
        /// Speak back text
        /// </summary>
        /// <param name="text">Text to speak</param>
        /// <param name="queue">If you want to chain together speak command or cancel current</param>
        /// <param name="crossLocale">Locale of voice</param>
        /// <param name="pitch">Pitch of voice</param>
        /// <param name="speechRate">Speak Rate of voice (All) (0.0 - 2.0f)</param>
        /// <param name="volume">Volume of voice (iOS/WP) (0.0-1.0)</param>
        public async Task SpeakAsync(string text, bool queue= false, CrossLocale? crossLocale = null, float? pitch = null , float? speechRate = null, float? volume= null)
        {
            if(!queue)
            { 
                if(_lockAsync != null)
                {
                    (await _lockAsync).Dispose();
                }
            }
            _lockAsync = _mutex.LockAsync();
            using (await _lockAsync)
            {
                if (string.IsNullOrWhiteSpace(text))
                    return;

                if (_speechSynthesizer == null)
                {
                    Init();
                }
                var localCode = string.Empty;

                //nothing fancy needed here
                if (pitch == null && speechRate == null && volume == null)
                {
                    if (crossLocale.HasValue && !string.IsNullOrWhiteSpace(crossLocale.Value.Language))
                    {
                        localCode = crossLocale.Value.Language;
                        var voices = from voice in SpeechSynthesizer.AllVoices
                                     where (voice.Language == localCode
                                     && voice.Gender.Equals(VoiceGender.Female))
                                     select voice;
                        _speechSynthesizer.Voice = (voices.Any() ? voices.ElementAt(0) : SpeechSynthesizer.DefaultVoice);
                        _element.Language = crossLocale.Value.Language;
                    }
                    else
                    {
                        _speechSynthesizer.Voice = SpeechSynthesizer.DefaultVoice;
                    }
                }

                else
                {
                    
                    _element.PlaybackRate = speechRate.Value;
                    _element.Volume = volume.Value;
                }

                try
                {
                    var stream = await _speechSynthesizer.SynthesizeTextToStreamAsync(text);

                    _element.SetSource(stream, stream.ContentType);
                    _element.MediaEnded += (s, e) => _tcs.TrySetResult(null);
                    _element.MediaFailed += (s, e) => _tcs.TrySetException(new Exception(e.ErrorMessage));


                    _element.Play();

                    await _tcs.Task;
                    _tcs = new TaskCompletionSource<object>();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Unable to playback stream: " + ex);
                    _tcs.TrySetException(new Exception(ex.Message));
                }
            }
        }
开发者ID:alexbazile,项目名称:MvxTTSPlugin,代码行数:77,代码来源:WindowsCommonTextToSpeech.cs

示例6: 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

示例7: TryCompleteFromCompletedTask_PropagatesException

        public void TryCompleteFromCompletedTask_PropagatesException()
        {
            Test.Async(async () =>
            {
                var source = new TaskCompletionSource();
                source.TrySetException(new NotImplementedException());

                var tcs = new TaskCompletionSource();
                tcs.TryCompleteFromCompletedTask(source.Task);
                await AssertEx.ThrowsExceptionAsync<NotImplementedException>(() => tcs.Task);
            });
        }
开发者ID:Lakerfield,项目名称:AsyncEx,代码行数:12,代码来源:TaskCompletionSourceExtensionsUnitTests.cs

示例8: TrySetExceptions_FaultsTask

 public void TrySetExceptions_FaultsTask()
 {
     var e = new[] { new InvalidOperationException() };
     var tcs = new TaskCompletionSource();
     tcs.TrySetException(e);
     Assert.IsTrue(tcs.Task.IsFaulted);
     Assert.IsTrue(tcs.Task.Exception.InnerExceptions.SequenceEqual(e));
 }
开发者ID:Lakerfield,项目名称:AsyncEx,代码行数:8,代码来源:TaskCompletionSourceUnitTests.cs


注:本文中的Nito.AsyncEx.TaskCompletionSource.TrySetException方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。