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


C# TimeSpan.AsThreadingTimeout方法代碼示例

本文整理匯總了C#中System.TimeSpan.AsThreadingTimeout方法的典型用法代碼示例。如果您正苦於以下問題:C# TimeSpan.AsThreadingTimeout方法的具體用法?C# TimeSpan.AsThreadingTimeout怎麽用?C# TimeSpan.AsThreadingTimeout使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.TimeSpan的用法示例。


在下文中一共展示了TimeSpan.AsThreadingTimeout方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ToObservable

        /// <summary>
        /// Converts a <see cref="System.Threading.WaitHandle"/> to an observable sequence
        /// </summary>
        /// <param name="waitHandle">the value to convert</param>
        /// <param name="executeOnlyOnce">true to observe multiple signals, false to complete the stream when signaled</param>
        /// <param name="timeout">optional timeout waiting for a single</param>
        /// <returns>A sequence that completes when <paramref name="executeOnlyOnce"/> is true, or never completes, and signals when false. Returns a TimeoutException when timeout is hit.</returns>
        public static IObservable<Unit> ToObservable(this WaitHandle waitHandle, bool executeOnlyOnce, TimeSpan? timeout = null)
        {
            return Observable.Create<Unit> (observer => {
                RegisteredWaitHandle registration = null;
                registration = ThreadPool.UnsafeRegisterWaitForSingleObject (
                                    waitHandle,
                                    (state, timedOut) => {
                                        if ((timedOut || executeOnlyOnce) && null != registration)
                                            registration.Unregister (waitHandle);

                                        if (timedOut)
                                            observer.OnError (new TimeoutException ());
                                        else {
                                            observer.OnNext (Unit.Default);

                                            if (executeOnlyOnce)
                                                observer.OnCompleted ();
                                        }
                                    },
                                    null,
                                    timeout.AsThreadingTimeout (),
                                    executeOnlyOnce);
                return () => registration.Unregister (waitHandle);
            });
        }
開發者ID:GeorgeTsiokos,項目名稱:corlib,代碼行數:32,代碼來源:WaitHandleExtensions.cs

示例2: ToTask

        /// <summary>
        /// Converts a <see cref="WaitHandle"/> into a disposable <see cref="Task"/>
        /// </summary>
        /// <param name="waitHandle">the operating-system specfic object to watch</param>
        /// <param name="timeout">optional timeout</param>
        /// <param name="asyncCallback">optional callback to call when the waitHandle signals</param>
        /// <param name="state">optional state to pass to the callback</param>
        /// <returns>An encapulted task - call Dispose on the result to unregister from the waitHandle's signal and cancel the task</returns>
        /// <remarks>Calling dispose on the task before it completes will result in an exception</remarks>
        public static IDisposable<Task> ToTask(this WaitHandle waitHandle, TimeSpan? timeout = null, AsyncCallback asyncCallback = null, object state = null)
        {
            var tcs = null == state ?
                new TaskCompletionSource<object> () :
                new TaskCompletionSource<object> (state);

            var registeredWaitHandle = ThreadPool.UnsafeRegisterWaitForSingleObject (
                waitHandle,
                (o, timedOut) => {
                    if (timedOut)
                        tcs.TrySetException (new TimeoutException ());
                    else
                        tcs.TrySetResult (o);
                },
                state,
                timeout.AsThreadingTimeout (),
                true);

            Action unregister = () => registeredWaitHandle.Unregister (waitHandle);
            Task task = tcs.Task.ContinueWith (_ => unregister ());
            IAsyncResult asyncResult = task;
            task = null == asyncCallback ?
                tcs.Task :
                tcs.Task.ContinueWith (_ => asyncCallback (asyncResult));

            return new DisposableValue<Task> (task, () => {
                unregister ();
                tcs.TrySetCanceled ();
            });
        }
開發者ID:GeorgeTsiokos,項目名稱:corlib,代碼行數:39,代碼來源:WaitHandleExtensions.cs


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