Click here to Skip to main content
15,893,923 members
Articles / Desktop Programming / WPF

Wrap Panel Virtualization

Rate me:
Please Sign up or sign in to vote.
4.95/5 (18 votes)
2 Jan 2012CPOL2 min read 53.6K   5.6K   41  
WrapPanel doesn't support virtualization. But we can improve the performance by simulating virtualization.
using System;
using System.ComponentModel;
using System.Runtime.Remoting;

namespace MefBasic.Threading
{
    public class BackgroundWorkerWrapper:IBackgroundWorker
    {
        private readonly BackgroundWorker _worker;

        public BackgroundWorkerWrapper()
        {
            _worker=new BackgroundWorker();
            _worker.Disposed += WorkerDisposed;
            _worker.DoWork += WorkerDoWork;
            _worker.ProgressChanged += WorkerProgressChanged;
            _worker.RunWorkerCompleted += WorkerRunWorkerCompleted;
        }

        void WorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            OnRunWorkerCompleted(e);
        }

        protected virtual void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
        {
            if (RunWorkerCompleted != null)
            {
                IsWorkerBusy = false;
                RunWorkerCompleted(this, e);
            }
        }

        void WorkerProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            OnProgressChanged(e);
        }

        protected virtual void OnProgressChanged(ProgressChangedEventArgs e)
        {
            if(ProgressChanged!=null)
            {
                ProgressChanged(this, e);
            }
        }

        void WorkerDoWork(object sender, DoWorkEventArgs e)
        {
            OnDoWork(e);
        }

        protected virtual void OnDoWork(DoWorkEventArgs args)
        {
            if(DoWork!=null)
            {
                IsWorkerBusy = true;
                DoWork(this, args);
                IsWorkerBusy = false;
            }
        }

        void WorkerDisposed(object sender, EventArgs e)
        {
            OnDisposed(e);
        }

        protected virtual void OnDisposed(EventArgs e)
        {
            if(Disposed!=null)
            {
                Disposed(this,e);
            }
        }

        #region Implementation of IBackgroundWorker

        public virtual void CancelAsync()
        {
            _worker.CancelAsync();
        }

        public virtual void ReportProgress(int percentProgress)
        {
            _worker.ReportProgress(percentProgress);
        }

        public virtual void ReportProgress(int percentProgress, object userState)
        {
            _worker.ReportProgress(percentProgress,userState);
        }

        public virtual void RunWorkerAsync()
        {            
            _worker.RunWorkerAsync();
        }

        public virtual void RunWorkerAsync(object argument)
        {
            _worker.RunWorkerAsync(argument);
        }
        public virtual bool CancellationPending
        {
            get { return _worker.CancellationPending; }
        }

        public virtual bool IsBusy
        {
            get { return _worker.IsBusy; }
        }

        public virtual bool WorkerReportsProgress
        {
            get { return _worker.WorkerReportsProgress; }
            set { _worker.WorkerReportsProgress=value; }
        }

        public virtual bool WorkerSupportsCancellation
        {
            get { return _worker.WorkerSupportsCancellation; }
            set { _worker.WorkerSupportsCancellation=value; }
        }

        public virtual ISite Site
        {
            get { return _worker.Site; }
            set { _worker.Site=value; }
        }

        public virtual IContainer Container
        {
            get { return _worker.Container; }
        }

        public bool IsWorkerBusy { get; set; }

        public event DoWorkEventHandler DoWork;
        public event ProgressChangedEventHandler ProgressChanged;
        public event RunWorkerCompletedEventHandler RunWorkerCompleted;
        public virtual void Dispose()
        {
            _worker.Dispose();
        }

        public event EventHandler Disposed;
        public virtual object GetLifetimeService()
        {
            return _worker.GetLifetimeService();
        }

        public virtual object InitializeLifetimeService()
        {
            return _worker.InitializeLifetimeService();
        }

        public virtual ObjRef CreateObjRef(Type requestedType)
        {
            return _worker.CreateObjRef(requestedType);
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) KAZ Software Limited
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions