internal void ProcessNativeCallback(
            CimOperationCallbackProcessingContext callbackProcessingContext,
            T currentItem,
            bool moreResults,
            MI_Result operationResult,
            string errorMessage,
            MI_Instance errorDetailsHandle)
        {
            Debug.Assert(callbackProcessingContext != null, "We should never get called with a null callbackProcessingContext");

            if (!moreResults)
            {
                this.DisposeOperationWhenPossible();
            }

            if ((currentItem == null) && (operationResult == MI_Result.MI_RESULT_OK))
            {
                // process the ACK message if and only if operationResult == OK
                if (this._reportOperationStarted)
                {
                    this.OnNextInternal(callbackProcessingContext, currentItem);
                }
            }
            else if (currentItem != null)
            {
                Debug.Assert(operationResult == MI_Result.MI_RESULT_OK, "Assumming that instances are reported back only on success");
                this.OnNextInternal(callbackProcessingContext, currentItem);
            }

            CimException exception = CimException.GetExceptionIfMiResultFailure(operationResult, errorMessage, errorDetailsHandle);

            if (exception != null)
            {
                Debug.Assert(operationResult != MI_Result.MI_RESULT_OK, "Assumming that exceptions are reported back only on failure");
                Debug.Assert(!moreResults, "Assumming that an error means end of results");

                // this throw-catch is needed to fill-out 1) WER data and 2) exception's stack trace
                try
                {
                    throw exception;
                }
                catch (CimException filledOutException)
                {
                    exception = filledOutException;
                }
            }

            if (!moreResults)
            {
                this.InvokeWhenOperationIsSet(
                    cimOperation => this.ProcessEndOfResultsWorker(callbackProcessingContext, cimOperation, exception));
            }
        }
        internal void ProcessNativeCallback(OperationCallbackProcessingContext callbackProcessingContext, T currentItem, bool moreResults, MiResult operationResult, string errorMessage, InstanceHandle errorDetailsHandle)
        {
            Action <CimOperation> action = null;

            if (!moreResults)
            {
                base.DisposeOperationWhenPossible();
            }
            if (currentItem != null || operationResult != MiResult.OK)
            {
                if (currentItem != null)
                {
                    this.OnNextInternal(callbackProcessingContext, currentItem);
                }
            }
            else
            {
                if (this._reportOperationStarted)
                {
                    this.OnNextInternal(callbackProcessingContext, currentItem);
                }
            }
            CimException exceptionIfMiResultFailure = CimException.GetExceptionIfMiResultFailure(operationResult, errorMessage, errorDetailsHandle);

            if (exceptionIfMiResultFailure != null)
            {
                try
                {
                    throw exceptionIfMiResultFailure;
                }
                catch (CimException cimException1)
                {
                    CimException cimException = cimException1;
                    exceptionIfMiResultFailure = cimException;
                }
            }
            if (!moreResults)
            {
                CimAsyncObserverProxyBase <T> cimAsyncObserverProxyBase = this;
                if (action == null)
                {
                    action = (CimOperation cimOperation) => this.ProcessEndOfResultsWorker(callbackProcessingContext, cimOperation, exceptionIfMiResultFailure);
                }
                cimAsyncObserverProxyBase.InvokeWhenOperationIsSet(action);
            }
        }
Exemple #3
0
        private bool MoveNext(bool discardResultsAndErrors)
        {
            T              t              = null;
            MiResult       miResult       = MiResult.OK;
            string         str            = null;
            InstanceHandle instanceHandle = null;
            bool           flag;

            if (!discardResultsAndErrors)
            {
                lock (this._internalErrorWhileProcessingAsyncCallbackLock)
                {
                    if (this._internalErrorWhileProcessingAsyncCallback != null)
                    {
                        throw this._internalErrorWhileProcessingAsyncCallback;
                    }
                }
            }
            if (this._moreResultsAreExpected)
            {
                this.AssertNotDisposed();
                MiResult miResult1 = this.NativeMoveNext(base.Operation.Handle, out t, out this._moreResultsAreExpected, out miResult, out str, out instanceHandle);
                CimException.ThrowIfMiResultFailure(miResult1);
                if (!this._moreResultsAreExpected)
                {
                    base.Operation.IgnoreSubsequentCancellationRequests();
                }
                if (!discardResultsAndErrors)
                {
                    lock (this._internalErrorWhileProcessingAsyncCallbackLock)
                    {
                        if (this._internalErrorWhileProcessingAsyncCallback != null)
                        {
                            throw this._internalErrorWhileProcessingAsyncCallback;
                        }
                    }
                    CimException exceptionIfMiResultFailure = CimException.GetExceptionIfMiResultFailure(miResult, str, instanceHandle);
                    if (exceptionIfMiResultFailure == null)
                    {
                        this.DisposeCurrentItemIfNeeded();
                        this.Current = t;
                    }
                    else
                    {
                        CancellationMode cancellationMode = base.Operation.CancellationMode;
                        if (cancellationMode != CancellationMode.ThrowOperationCancelledException)
                        {
                            throw exceptionIfMiResultFailure;
                        }
                        else
                        {
                            throw new OperationCanceledException(exceptionIfMiResultFailure.Message, exceptionIfMiResultFailure);
                        }
                    }
                }
                else
                {
                    IDisposable disposable = (object)t as IDisposable;
                    if (disposable != null)
                    {
                        disposable.Dispose();
                        t = default(T);
                    }
                    if (instanceHandle != null)
                    {
                        instanceHandle.Dispose();
                    }
                }
                if (t == null)
                {
                    flag = this._moreResultsAreExpected;
                }
                else
                {
                    flag = true;
                }
                return(flag);
            }
            else
            {
                return(false);
            }
        }
        public bool MoveNext()
        {
            lock (_disposeThreadSafetyLock)
            {
                lock (this._internalErrorWhileProcessingAsyncCallbackLock)
                {
                    if (this._internalErrorWhileProcessingAsyncCallback != null)
                    {
                        throw this._internalErrorWhileProcessingAsyncCallback;
                    }
                }

                if (!_moreResultsAreExpected)
                {
                    return(false);
                }

                this.AssertNotDisposed();

                T           currentItem;
                MI_Result   result;
                string      errorMessage;
                MI_Instance errorDetailsHandle;
                MI_Result   functionResult = NativeMoveNext(
                    this.Operation.Handle,
                    out currentItem,
                    out this._moreResultsAreExpected,
                    out result,
                    out errorMessage,
                    out errorDetailsHandle);

                CimException.ThrowIfMiResultFailure(functionResult);
                if (!this._moreResultsAreExpected)
                {
                    this.Operation.IgnoreSubsequentCancellationRequests();
                }

                lock (this._internalErrorWhileProcessingAsyncCallbackLock)
                {
                    if (this._internalErrorWhileProcessingAsyncCallback != null)
                    {
                        throw this._internalErrorWhileProcessingAsyncCallback;
                    }
                }

                CimException cimException = CimException.GetExceptionIfMiResultFailure(result, errorMessage,
                                                                                       errorDetailsHandle);
                if (cimException != null)
                {
                    CancellationMode cancellationMode = this.Operation.CancellationMode;
                    Debug.Assert(cancellationMode != CancellationMode.SilentlyStopProducingResults,
                                 "CancellationMode.SilentlyStopProducingResults is only applicable to IObservable pattern");
                    if (cancellationMode == CancellationMode.ThrowOperationCancelledException)
                    {
                        throw new OperationCanceledException(cimException.Message, cimException);
                    }
                    else
                    {
                        throw cimException;
                    }
                }

                Debug.Assert(result == MI_Result.MI_RESULT_OK, "Exception should be thrown above in case of error");
                this.DisposeCurrentItemIfNeeded();
                this.Current = currentItem;

                bool currentResultsIsValid;
                if (currentItem != null)
                {
                    currentResultsIsValid = true;
                }
                else
                {
                    // If more results are expected, then we have to treat currentItem=null as a valid result
                    currentResultsIsValid = this._moreResultsAreExpected;

                    // Native MI API contract is that CurrentItem=null + moreResults=true should only happen 1) as the first result and 2) when CimOperationFlags.ReportOperationStarted was used.
                    // Unfortunately operation flags and knowledge whether we have first or subsequent result is not available at this point - we cannot assert the statement above :-(
                }
                return(currentResultsIsValid);
            }
        }