예제 #1
0
        public async void LoadImageAsync(int priority, EventHandler BitmapLoaded = null, CancellationTokenSource cancel = null)
        {
            if (BitmapLoaded != null)
            {
                _bitmapLoadedHandler = BitmapLoaded;
            }
            if ((IsImageLoaded && BitmapLoaded == null) || IsImageLoading)
            {
                if (_dataLoaderTask.Status == TaskStatus.RanToCompletion && BitmapLoaded != null)
                {
                    try
                    {
                        Application.Current.Dispatcher.InvokeIfRequired(DispatcherPriority.Normal, new Action(() =>
                        {
                            if (_bitmapLoadedHandler != null)
                            {
                                _bitmapLoadedHandler(this, EventArgs.Empty);
                            }
                        }));
                    }
                    catch (Exception) { }
                    finally { _bitmapLoadedHandler = null; }
                }
                return;
            }

            lock (_lock) { _cancelImageLoad = (cancel != null) ? cancel : new CancellationTokenSource(); }
            _dataLoaderTask = BblTask.Run(() => Book.LoadPageData(Index, _cancelImageLoad.Token), priority, _cancelImageLoad.Token);
            try
            {
                await _dataLoaderTask;
                if (_bitmapLoadedHandler != null)
                {
                    try {
                        Application.Current.Dispatcher.InvokeIfRequired(DispatcherPriority.Normal, new Action(() =>
                        {
                            if (_bitmapLoadedHandler != null)
                            {
                                _bitmapLoadedHandler(this, EventArgs.Empty);
                            }
                        }));
                    }
                    catch (Exception) { }
                    finally { _bitmapLoadedHandler = null; }
                }
                try {
                    if (IsThumbnailLoaded == true && ThumbnailLoaded != null)
                    {
                        Application.Current.Dispatcher.InvokeIfRequired(DispatcherPriority.Normal, new Action(() =>
                        {
                            if (ThumbnailLoaded != null)
                            {
                                ThumbnailLoaded(this, new EventArgs());
                            }
                        }));
                    }
                }
                catch { }
            }
            catch
            {
                //if(!(e is InvalidOperationException && e.HResult == -2146233079))
                lock (_lock)
                {
                    if (Image != null)
                    {
                        Image.Dispose();
                    }
                    Image = null;
                }
            }
            finally
            {
                lock (_lock)
                {
                    _dataLoaderTask = null;
                    if (cancel == null)
                    {
                        _cancelImageLoad.Dispose();
                        _cancelImageLoad = null;
                    }
                }
            }
        }
예제 #2
0
        public async void PopulateAsync(int priority)
        {
            if (IsPopulating)
            {
                return;
            }
            if (IsPopulated && !IsPopulating)
            {
                if (IsThumbnailLoaded || IsThumbnailLoading)
                {
                    Populated(this, new EventArgs());
                }
                else
                {
                    LoadThumbnail(priority);
                }
                return;
            }

            _cancelPopulateTask = new CancellationTokenSource();
            try
            {
                IProgress <bool> progress = new Progress <bool>((populated) =>
                {
                    if (populated)
                    {
                        Populated(this, new EventArgs());
                    }
                    else
                    {
                        Root.OnBookOperation(new BookOperationData(this, BookOperations.Remove, null));
                        _demoted = true;
                        if (Demoted != null)
                        {
                            Demoted(this, new EventArgs());
                        }
                    }
                });

                await(_populateTask = BblTask.Run(() =>
                {
                    try
                    {
                        _cancelPopulateTask.Token.ThrowIfCancellationRequested();
                        Populate();
                        _cancelPopulateTask.Token.ThrowIfCancellationRequested();

                        if (_pages != null && _pages.Count > 0)
                        {
                            DeserializeIVP();
                            progress.Report(true);
                            _pages[0].ThumbnailLoaded += OnThumbnailLoaded;
                            _pages[0].LoadImageAsync(priority);
                        }
                        else
                        {
                            UnPopulate();
                            progress.Report(false);
                        }
                        _cancelPopulateTask.Token.ThrowIfCancellationRequested();
                    }
                    catch /*(Exception e)*/ { UnPopulate(); }
                    finally
                    {
                        lock (_lock)
                        {
                            _populateTask = null;
                            if (_cancelPopulateTask != null)
                            {
                                _cancelPopulateTask.Dispose();
                            }
                            _cancelPopulateTask = null;
                        }
                    }
                }
                                                  , priority, _cancelPopulateTask.Token));
            }
            catch
            {
                UnPopulate();
            }
        }