Click here to Skip to main content
11,482,402 members (75,228 online)
Click here to Skip to main content
Add your own
alternative version

Another Screensaver with WPF

, 27 Jan 2012 CDDL 37.8K 3.4K 60
Lessons learnt from writing a screensaver with WPF
using System;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using RZWScreenSaver.Properties;

namespace RZWScreenSaver{
    class BackgroundSlideShowEngine{
        public BackgroundSlideShowEngine(IPictureSource source){
            pictureSource = source;

            screenSaverCheck.Interval = TimeSpan.FromSeconds(5);
            screenSaverCheck.Tick += onCheckScreenSaver;
        void onCheckScreenSaver(object sender, EventArgs e){
            bool isRunning;
            var success = Win32.SystemParametersInfo(Win32.SPI_GETSCREENSAVERRUNNING, 0, out isRunning, 0);
            if (isRunning ^ pictureSource.IsPaused)
                if (isRunning)
        public void Start(PageHost[] slideShowList){
            var engineAssemblyPath = Assembly.GetExecutingAssembly().Location;
            Debug.Assert(engineAssemblyPath != null);
            var aboutDomain = AppDomain.CreateDomain("Background Domain");
            var helperTypeName = typeof (ForegroundDomain).FullName;
            var foregroundDomain = (ForegroundDomain) aboutDomain.CreateInstanceFromAndUnwrap(engineAssemblyPath,helperTypeName);
            foregroundDomain.MainApplication = new SaverEngine(pictureSource, slideShowList);

            var aboutThread = new Thread(foregroundDomain.RunAbout);

        public class SaverEngine : MarshalByRefObject, ScreenSaverEngine.ISaverEngine, IDisposable{
            public SaverEngine(IPictureSource source, PageHost[] slideShowList){
                pictureSource = source;
                this.slideShowList = slideShowList;
            public void SwitchToSet(int setIndex){
                                                           new Action<int>(pictureSource.SwitchToSet), setIndex);
                Settings.Default.BackgroundPictureSetSelected = setIndex;
            public void ToggleShowTitle(){
                Settings.Default.ShowTitle = !Settings.Default.ShowTitle;
                foreach (var host in slideShowList){
                                                               new Action<PageHost>(setHostTitle), host);
            public void Dispose(){
            public override object InitializeLifetimeService() {
                return null;
            /// <summary>
            /// must be called from its thread
            /// </summary>
            void setHostTitle(PageHost host){
                host.SlidePage.ShowTitle = Settings.Default.ShowTitle;
            readonly IPictureSource pictureSource;
            readonly PageHost[] slideShowList;
        public class ForegroundDomain : MarshalByRefObject{
            public SaverEngine MainApplication { get; set; }
            public void RunAbout(){
                Debug.WriteLine("App Domain Name: " + AppDomain.CurrentDomain.FriendlyName);
                Debug.WriteLine("Has Application? " + (Application.Current != null));
                var aboutDialog = new AboutRZ(MainApplication);
                new Application{ShutdownMode = ShutdownMode.OnExplicitShutdown}.Run();
                if (MainApplication != null)
            public override object InitializeLifetimeService() {
                return null;
        readonly DispatcherTimer screenSaverCheck = new DispatcherTimer(DispatcherPriority.Background);
        readonly IPictureSource pictureSource;

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.


This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


About the Author

Ruxo Zheng
Technical Lead
Thailand Thailand
C/C++ and C# programmer.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 27 Jan 2012
Article Copyright 2009 by Ruxo Zheng
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid