Example #1
0
        void ShowInternal(IDocumentTabContent tabContent, object serializedUI, Action <ShowTabContentEventArgs> onShownHandler, bool isRefresh)
        {
            Debug.Assert(asyncWorkerContext == null);
            var oldUIContext = UIContext;

            UIContext = tabContent.CreateUIContext(documentTabUIContextLocator);
            var cachedUIContext = UIContext;

            Debug.Assert(cachedUIContext.DocumentTab == null || cachedUIContext.DocumentTab == this);
            cachedUIContext.DocumentTab = this;
            Debug.Assert(cachedUIContext.DocumentTab == this);
            Debug.Assert(tabContent.DocumentTab == null || tabContent.DocumentTab == this);
            tabContent.DocumentTab = this;
            Debug.Assert(tabContent.DocumentTab == this);

            UpdateTitleAndToolTip();
            var showCtx = new ShowContext(cachedUIContext, isRefresh);

            tabContent.OnShow(showCtx);
            bool asyncShow       = false;
            var  asyncTabContent = tabContent as IAsyncDocumentTabContent;

            if (asyncTabContent != null)
            {
                if (asyncTabContent.CanStartAsyncWorker(showCtx))
                {
                    asyncShow = true;
                    var ctx = new AsyncWorkerContext();
                    asyncWorkerContext = ctx;
                    Task.Factory.StartNew(() => asyncTabContent.AsyncWorker(showCtx, ctx.CancellationTokenSource), ctx.CancellationToken)
                    .ContinueWith(t => {
                        bool canShowAsyncOutput = ctx == asyncWorkerContext &&
                                                  cachedUIContext.DocumentTab == this &&
                                                  UIContext == cachedUIContext;
                        if (asyncWorkerContext == ctx)
                        {
                            asyncWorkerContext = null;
                        }
                        ctx.Dispose();
                        asyncTabContent.EndAsyncShow(showCtx, new AsyncShowResult(t, canShowAsyncOutput));
                        bool success = !t.IsFaulted && !t.IsCanceled;
                        OnShown(serializedUI, onShownHandler, showCtx, success);
                    }, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
                }
                else
                {
                    asyncTabContent.EndAsyncShow(showCtx, new AsyncShowResult());
                }
            }
            if (!asyncShow)
            {
                OnShown(serializedUI, onShownHandler, showCtx, true);
            }
            documentTabService.OnNewTabContentShown(this);
        }
Example #2
0
        void ShowInternal(DocumentTabContent tabContent, object uiState, Action <ShowTabContentEventArgs> onShownHandler, bool isRefresh)
        {
            Debug.Assert(asyncWorkerContext == null);
            UIContext = tabContent.CreateUIContext(documentTabUIContextLocator);
            var cachedUIContext = UIContext;

            Debug.Assert(cachedUIContext.DocumentTab == null || cachedUIContext.DocumentTab == this);
            cachedUIContext.DocumentTab = this;
            Debug.Assert(cachedUIContext.DocumentTab == this);
            Debug.Assert(tabContent.DocumentTab == null || tabContent.DocumentTab == this);
            tabContent.DocumentTab = this;
            Debug.Assert(tabContent.DocumentTab == this);

            UpdateTitleAndToolTip();
            DnSpyEventSource.Log.ShowDocumentTabContentStart();
            var showCtx = new ShowContext(cachedUIContext, isRefresh);

            tabContent.OnShow(showCtx);
            bool asyncShow = false;

            if (tabContent is AsyncDocumentTabContent asyncTabContent)
            {
                if (asyncTabContent.NeedAsyncWork(showCtx))
                {
                    asyncShow = true;
                    var ctx = new AsyncWorkerContext();
                    asyncWorkerContext = ctx;
                    var asyncShowCtx = new AsyncShowContext(showCtx, ctx);
                    Task.Run(() => asyncTabContent.CreateContentAsync(asyncShowCtx), ctx.CancellationToken)
                    .ContinueWith(t => {
                        bool canShowAsyncOutput = ctx == asyncWorkerContext &&
                                                  cachedUIContext.DocumentTab == this &&
                                                  UIContext == cachedUIContext;
                        if (asyncWorkerContext == ctx)
                        {
                            asyncWorkerContext = null;
                        }
                        ctx.Dispose();
                        asyncTabContent.OnShowAsync(showCtx, new AsyncShowResult(t, canShowAsyncOutput));
                        bool success = !t.IsFaulted && !t.IsCanceled;
                        OnShown(uiState, onShownHandler, showCtx, success ? ShowTabContentResult.ShowedContent : ShowTabContentResult.Failed);
                    }, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
                }
                else
                {
                    asyncTabContent.OnShowAsync(showCtx, new AsyncShowResult());
                }
            }
            if (!asyncShow)
            {
                OnShown(uiState, onShownHandler, showCtx, ShowTabContentResult.ShowedContent);
            }
            documentTabService.OnNewTabContentShown(this);
        }