Click here to Skip to main content
11,578,524 members (60,840 online)
Click here to Skip to main content
Add your own
alternative version

Wrap Panel Virtualization

, 2 Jan 2012 CPOL 22K 3.4K 35
WrapPanel doesn't support virtualization. But we can improve the performance by simulating virtualization.
mediaassistant_bin-noexe.zip
MediaAssistant_bin.zip
MediaAssistant_src
DokanNet.dll
Id3Lib.dll
Media Assistant.exe
Media Assistant.exe.manifest
MediaAssistant.DAL.dll
MediaFS.dll
MefBasic.dll
Microsoft.Practices.Composite.dll
Microsoft.Practices.Composite.MefExtensions.dll
Microsoft.Practices.Composite.Presentation.dll
Microsoft.Practices.ServiceLocation.dll
Mp3Lib.dll
Newtonsoft.Json.Net35.dll
SharpZipLib.dll
Tags.dll
mediaassistant_src-noexe.zip
MediaAssistant_src.zip
DokanNet
Properties
Lib
CommonServiceLocation
Desktop
Microsoft.Practices.ServiceLocation.dll
CompositeApplicationLibrary
Desktop
Microsoft.Practices.Composite.dll
Microsoft.Practices.Composite.MefExtensions.dll
Microsoft.Practices.Composite.Presentation.dll
Dokan
dokan.lib
Moq
Desktop
Moq.dll
Mp3Lib
Id3Lib.dll
Mp3Lib.dll
SharpZipLib.dll
Newtonsoft
Newtonsoft.Json.Net35.dll
NUnit
nunit.framework.dll
MediaAssistant
MediaAssistant.csproj.user
MediaAssistant.ico
MusicAssistant_TemporaryKey.pfx
Constants
Controls
About
AddMovie
AlternativeLocation
BottomPanel
Dialog
LeftPanel
Library
MessageBox
MovieDetail
MovieList
MovieMiddlePanel
MovieResult
MovieSearchControl
MovieThumbnails
MusicList
MusicMiddlePanel
MusicSearchControl
PlayerControl
PlayingMusicInfo
PlaylistSelector
ProfileSelector
SendFeedback
SmartDJPreferance
SplashScreen
SplashPage.png
StatusMessageBar
TagEditor
TopPanel
WaitScreen
Converters
Data
EventArguments
HeaderImages
Close_act.png
Close_inact.png
Close_pr.png
Maximize_act.png
Maximize_inact.png
Maximize_pr.png
Minimize_act.png
Minimize_inact.png
Minimize_pr.png
Helper
Images
AcceptFolder.png
Actor.png
ActorLibrary.png
AddFolder.png
Album.png
Artist.png
Close.png
Composer.png
Default.png
DeleteFolder.png
Director.png
DrivesMovies.png
eye16.png
FailedToPlayMusic.png
Genre.png
IMDB.png
Library.png
ListView.png
Logo.png
Maximize.png
Menu.png
Mount.png
MovieBackground.jpg
MovieBackground.png
MovieFolder.png
MovieGenre.png
MovieLibrary.png
MoviePoster.png
Music.png
MusicAssistant.png
MusicLibrary.png
Mute.png
New.png
NewMovies.png
NoDJ.png
NonPlayingMusic.png
NormalShuffle.png
NoShuffle.png
NowPlayingMusic.png
PlayingMusic.png
playlist.png
PlayMovie.png
ProcessDone.png
ProcessFailed.png
Processing.png
Rated.png
Recent.png
RecommendedMovie.png
Restore.png
Runtime.png
SmartShuffle.png
Song.png
Star.png
Swap.png
ThumbnailView.png
Tools.png
TreeCollapse.png
TreeExpand.png
Unread.png
Volume.png
WatchList.png
WishList.png
Writer.png
Year.png
Management
MusicDJ
Properties
Settings.settings
Resources
MediaAssistant.DAL
MediaAssistant.sdf
MediaAssistantModels.edmx
Constants
DBUpdateScript
Helper
Properties
Settings.settings
DataSources
MusicAssistantEntities.datasource
TasteKidEntities.datasource
Virtualization
MediaAssistant.Setup
MusicAssistant.Setup.vdproj
MediaFS
DokanInstall_0.6.0.exe
Properties
MefBasic
Behaviors
Commans
Controls
Converters
Data
Extensions
Helper
Properties
Settings.settings
Themes
Threading
UserControls
Properties
TagClass
ASF Classes
Frames Classes
ID3
Properties
Text Files
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Threading;

namespace MefBasic.Threading
{
    public class Job 
    {
        private int _maxWaitTimeToStop;
        public Action<Job> JobAction { get; private set; }
        public Action<JobProgressArgs> ProgressNotifyAction { get; private set; }
        public event EventHandler JobCompleted;
        public event CancelEventHandler StartingJob;
        protected List<string> DependentJobs { get; set; }
        protected void InvokeStartingJob(CancelEventArgs e)
        {
            if (StartingJob != null) 
                StartingJob(this, e);
        }

        public bool IsThreadSafe { get; set; }
        public JobStore Store { get; set; }
        public string Id { get; set; }
        public AutoResetEvent WaitEvent { private set; get; }

        public Job(Action<Job> jobAction)
            : this(jobAction, null)
        {
            MaxWaitTimeToStop = 5000;
        }
        public Job Dependency(params string [] dependentJobs)
        {
            DependentJobs.AddRange(dependentJobs);
            return this;
        }
        public Job(Action<Job> jobAction, Action<JobProgressArgs> progressNotifyAction)
        {
            ProgressNotifyAction = progressNotifyAction;
            JobAction = jobAction;
            Id = Guid.NewGuid().ToString();
            WaitEvent = new AutoResetEvent(false);
            Store = new JobStore {Status = JobStatus.NotStarted};
            ChildreenJob = new List<Job>();
            IsThreadSafe = true;
            DependentJobs=new List<string>();
        }

        
        protected void OnJobCompleted(EventArgs e)
        {
            switch (Store.Status)
            {
                case JobStatus.AbortRequested:
                    Store.Status = JobStatus.Aborted;
                    break;
                case JobStatus.Started:
                    Store.Status = JobStatus.Completed;
                    break;
            }
            IsComplete = true;
            if (JobCompleted != null)
                JobCompleted(this, e);
        }
        
        protected void OnProgressAction(JobProgressArgs args)
        {
            if (ProgressNotifyAction != null)
            {
                ProgressNotifyAction(args);
            }
        }

        public virtual void Start()
        {
            if(InvokeStartingJob())
                return;
            DoStart();
        }

        public void Resume()
        {
            if (Store.PauseRequested)
            {
                Store.PauseRequested = false;
                Store.Status = JobStatus.Started;
                return;
            }
        }

        protected void DoStart()
        {
            Store.Status = JobStatus.Started;
            WaitEvent.Reset();
            if (IsThreadSafe)
            {
                
                var worker = new BackgroundWorker();
                worker.DoWork += (s, e) => DoWork(e.Argument);
                worker.RunWorkerCompleted += (s, e) =>
                                                  {
                                                      WaitEvent.Set();
                                                      OnJobCompleted(new EventArgs());
                                                  };
                worker.RunWorkerAsync(this);
            }
            else
            {
                DoWork(this);
                WaitEvent.Set();
                OnJobCompleted(new EventArgs());
            }
        }

        protected bool InvokeStartingJob()
        {
            var cancelEventArgs = new CancelEventArgs(false);
            InvokeStartingJob(cancelEventArgs);
            var isCancelled = cancelEventArgs.Cancel;
            if(isCancelled)
            {
                Store.Status = JobStatus.Aborted;
                WaitEvent.Set();
                OnJobCompleted(new EventArgs());
            }
            return isCancelled;
        }

        private void DoWork(object args)
        {
            WaitForDependentJobs();
            var job = (Job) args;
            if (ChildreenJob.Count > 0)
            {
                foreach (var childJob in ChildreenJob)
                {
                    childJob.Start();
                }
                ChildreenJob.ForEach(c=>c.Wait());
            }
            if(JobAction!=null)
                JobAction(job);
        }

        protected void WaitForDependentJobs()
        {
            foreach (var dependentJob in DependentJobs)
            {
                Parent.GetChild(dependentJob).Wait();
            }
        }

        public bool IsComplete { get; protected set; }
        public void Wait()
        {
            if (IsComplete) return;
            if (IsComplete == false)
            {
                WaitEvent.WaitOne();
                WaitEvent.Set();
            }
        }

        protected List<Job> ChildreenJob { get; set; }

        public void AddChild(Job childJob)
        {
            childJob.Parent = this;
            ChildreenJob.Add(childJob);
        }

        public Job Parent { get; set; }

        public Job Root
        {
            get
            {
                return Parent == null ? this : Parent.Root;
            }
        }

        public int MaxWaitTimeToStop
        {
            get { return _maxWaitTimeToStop; }
            set { _maxWaitTimeToStop = value; }
        }

        public IEnumerable<Job> GetChildreen()
        {
            return ChildreenJob;
        }

        public void NotifyProgress()
        {
            OnProgressAction(new JobProgressArgs { Sender = this });
        }

        public void Abort()
        {
            Store.Status = JobStatus.AbortRequested;
            ChildreenJob.ForEach(c=>c.Abort());
        }

        public Job GetChild(string childId)
        {
            return GetChildreen().FirstOrDefault(child => child.Id == childId);
        }

        public void NotifyProgress(int progress)
        {
            OnProgressAction(new JobProgressArgs { Sender = this,Progress = progress});
        }

        public void StartBlocked()
        {
            var worker = new BackgroundWorkerEx();
            worker.DoWork += (s, e) =>
                                 {
                                     Start();
                                     Wait();
                                 };
            worker.RunWorkerBlocked();
        }

        public Job GetSibling(string id)
        {
            return Parent.GetChild(id);
        }

        public void Stop()
        {
            Store.StopRequested = true;
            var stopWatch = new Stopwatch();
            stopWatch.Start();
            while (!IsComplete&&stopWatch.ElapsedMilliseconds<MaxWaitTimeToStop)
            {
                DoEvents();
            }
        }
        private static void DoEvents()
        {
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));
        }

        public void Pause()
        {
            Store.PauseRequested = true;
            var stopWatch = new Stopwatch();
            stopWatch.Start();
            while (Store.Status!=JobStatus.Paused && stopWatch.ElapsedMilliseconds < MaxWaitTimeToStop)
            {
                DoEvents();
            }
        }
    }
}

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)

Share

About the Author

H. S. Masud
Software Developer (Senior) KAZ Software Limited
Bangladesh Bangladesh
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 2 Jan 2012
Article Copyright 2012 by H. S. Masud
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid