Esempio n. 1
0
        /// <summary>
        /// Called on a new thread to process events from the graph.  The thread
        /// exits when the graph finishes.  Cancelling is done here.
        /// </summary>
        private void EventWait()
        {
            try
            {
                // Returned when GetEvent is called but there are no events
                const int E_ABORT = unchecked ((int)0x80004004);

                int       hr;
                IntPtr    p1, p2;
                EventCode ec;
                // TODO: present the event code in the OnCompleted event
                EventCode exitCode;
                var       pEvent = (IMediaEvent)Graph;

                do
                {
                    // Read the event
                    for (
                        hr = pEvent.GetEvent(out ec, out p1, out p2, 100);
                        hr >= 0 && _state < RendererState.GraphCompleted;
                        hr = pEvent.GetEvent(out ec, out p1, out p2, 100)
                        )
                    {
                        switch (ec)
                        {
                        // If the clip is finished playing
                        case EventCode.Complete:
                        case EventCode.ErrorAbort:
                            ChangeState(RendererState.GraphStarted, RendererState.GraphCompleting);
                            exitCode = ec;

                            // Release any resources the message allocated
                            hr = pEvent.FreeEventParams(ec, p1, p2);
                            DESError.ThrowExceptionForHR(hr);
                            break;

                        default:
                            // Release any resources the message allocated
                            hr = pEvent.FreeEventParams(ec, p1, p2);
                            DESError.ThrowExceptionForHR(hr);
                            break;
                        }
                    }

                    // If the error that exited the loop wasn't due to running out of events
                    if (hr != E_ABORT)
                    {
                        DESError.ThrowExceptionForHR(hr);
                    }
                } while (_state < RendererState.GraphCompleting);

                // If the user canceled
                if (_state == RendererState.Cancelling)
                {
                    // Stop the graph, send an appropriate exit code
                    hr       = _mediaControl.Stop();
                    exitCode = EventCode.UserAbort;
                }

                if (_state == RendererState.GraphCompleting)
                {
                    ChangeState(RendererState.GraphCompleted);
                    OnRenderCompleted();
                    if (_renderResult != null)
                    {
                        _renderResult.Complete(false);
                    }
                    if (_cancelResult != null)
                    {
                        _cancelResult.Complete(false);
                    }
                }
                else
                {
                    ChangeState(RendererState.Canceled);
                    OnRenderCompleted();
                    if (_renderResult != null)
                    {
                        _renderResult.Complete(true);
                    }
                    if (_cancelResult != null)
                    {
                        _cancelResult.Complete(false);
                    }
                }
            }
            catch (COMException ex)
            {
                if (_renderResult != null)
                {
                    _renderResult.Complete(ex);
                }
                if (_cancelResult != null)
                {
                    _cancelResult.Complete(ex);
                }
            }
        }