Click here to Skip to main content
15,891,372 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.5K   5.6K   41  
WrapPanel doesn't support virtualization. But we can improve the performance by simulating virtualization.
using System;
using System.ComponentModel.Composition;
using System.Threading;
using MediaAssistant.Data;
using MefBasic.Threading;

namespace MediaAssistant.Management
{
    public abstract class BackgroundScanner
    {
        protected Job Job { get; set; }

        private void CreateJob()
        {
            Job = new Job(StartScanner);
        }
        [Import]
        protected Lazy<LibraryDataSource> DataSource { get; set; }
        [Import]
        protected StatusMessageService StatusMessageService { get; set; }
 
        protected string Name { get; set; }
        protected bool CancelPendingJobRequested { get; set; }

        public void Start()
        {
            CreateJob();
            Job.Start();
        }

        public virtual void Stop()
        {
            if (Job==null)
                return;
            Job.Stop();
            Job = null;
        }

        protected void SetStatusMessage(string message)
        {
            //Note: Because of status message Library scrolled to top. I don't know why.
            //return;
            StatusMessageService.SetStatusMessage(message);
        }

        public void Pause()
        {
            if (Job == null) 
                return;
            SetStatusMessage(string.Format("Pausing {0}", Name));
            Job.Pause();
            SetStatusMessage(string.Format("{0} Paused", Name));
        }

        public void Resume()
        {
            if(Job ==null)
                return;
            Job.Resume();
            SetStatusMessage(string.Format("{0} Resumed",Name));
        }

        protected void StartScanner(Job obj)
        {
            while (Job.Store.StopRequested==false)
            {
                if (Job.Store.StopRequested)
                {
                    return;
                }
                Thread.Sleep(1000);
                PauseIfRequested();
                try
                {
                    TryToScan();
                }
                catch (Exception)
                {
                    
                }
            }
        }

        protected abstract void TryToScan();

        protected virtual bool ShallReturn()
        {
            bool shallReturn = false;
            if (Job.Store.StopRequested)
            {
                shallReturn = true;
            }
            Thread.Sleep(100);
            return shallReturn;
        }

        protected virtual bool PauseIfRequested()
        {
            if (Job.Store.PauseRequested)
            {
                Job.Store.Status = JobStatus.Paused;
                while (Job.Store.PauseRequested)
                {
                    if(ShallReturn())
                        return true;
                    Thread.Sleep(100);
                }
            }
            return false;
        }

        protected void CancelPendingJob()
        {
            CancelPendingJobRequested = true;
            while (CancelPendingJobRequested)
            {
                Thread.Sleep(100);
            }
        }
    }
}

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