Click here to Skip to main content
15,891,567 members
Articles / Desktop Programming / WPF

Create MVVM Background Tasks with Progress Reporting

Rate me:
Please Sign up or sign in to vote.
4.93/5 (34 votes)
9 Sep 2011CPOL18 min read 93.5K   6.1K   85  
This article explains how to implement background processing and parallel processing for time consuming tasks in MVVM, and how to create a progress dialog with cancellation for those tasks.
#region Copyright Notice

// Distributed under the Code Project Open License 
// ****************************************************
// 
// Copyright � 2011. David C. Veeneman
// All rights reserved 
// 
// Redistribution and use on source and binary forms, with or without modification 
// are permitted provided that the following conditions are met:
// 
// � Redistributions of source code must retain the above copyright notice, this 
// list of conditions and the following disclaimers. 
// 
// � Redistributions on binary form must reproduce the above copyright notice, this 
// list of conditions and the following disclaimer in the documentation anclior other 
// materials provided with the distribution. 
// 
// � Neither the name of David C. Veeneman nor Foresight Systems may be used to endorse 
// or promote products derived from this software without specific prior written permission. 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, 
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID C VEENEMAN OR FORERSIGHT SYSTEMS BE 
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSITTUIE GOODS OR SERVICES: LOSS OF USE, DATA OR PROFITS; OR 
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
// LIABILITY, OR TORT. 

#endregion

using System;
using System.Windows;
using System.Windows.Controls;
using WpfProgressDemo.ViewModel;

namespace WpfProgressDemo.View.Services
{
    /// <summary>
    /// Provides services for the View.
    /// </summary>
    public static class ViewServices
    {
        #region Fields

        // Member variables
        private static ProgressDialog m_ProgressDialog;

        #endregion

        #region Service Methods

        /// <summary>
        /// Hides the Progress dialog.
        /// </summary>
        internal static void CloseProgressDialog()
        {
            m_ProgressDialog.Close();
        }

        /// <summary>
        /// Shows the Progress dialog.
        /// </summary>
        internal static void ShowProgressDialog(Window mainWindow, ProgressDialogViewModel viewModel)
        {
            m_ProgressDialog = new ProgressDialog();
            m_ProgressDialog.Owner = mainWindow;
            m_ProgressDialog.DataContext = viewModel;
            m_ProgressDialog.Show();
        }

        #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) Foresight Systems
United States United States
David Veeneman is a financial planner and software developer. He is the author of "The Fortune in Your Future" (McGraw-Hill 1998). His company, Foresight Systems, develops planning and financial software.

Comments and Discussions