Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7819795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:04:40+00:00 2026-06-02T07:04:40+00:00

I’m trying to implement my first application using the MVVM pattern. I’ve manged to

  • 0

I’m trying to implement my first application using the MVVM pattern. I’ve manged to get most things working, but now I’m facing a problem with the following (IMHO pretty common) scenario:

Pressing a Button (View) shall invoke a Method (Model). Using a ICommand (ViewModel) this is pretty easy. But what to do if a time consuming operation has to be executed?

My current solution required me to implement a WorkQueue class containing WorkQueueItems. The WorkQueue has a Thread associated with it which executes the WorkQueueItems. Each WorkQueueItem has a Name, a Status and a Progress which is updated during execution.
Each Window has its own WorkQueue – visualized as StatusBar.

My problem: How can a ViewModel find the appropriate WorkQueue? Do I have to pass the WorkQueue to each ViewModel I create (this would be really be annoying)? Or are there other mechanism I could use?

I’m not really familiar with RoutedCommands – tough the basic concept seems to go into this direction. What’d love to see is a solution where I can bind a WorkQueueItem to a Command/Event which then bubbles up to the containing Window where it is added to the Window‘s WorkQueue.

I also considered making WorkQueue a Singleton – but this only works if I only have one Window at a time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:04:42+00:00Added an answer on June 2, 2026 at 7:04 am

    With the later .Net Frameworks (4.0+) and WPF you can utilize the System.Threading.Tasks library to provide a lot of this work under the hood.

    If say your Command on your needs to update a property on your View Model, but it has to wait for the information, you simply start a task to perform the IO:

    this.FindDataCommand = new RelayCommand<string>(
        /* ICommand.Execute */
        value =>
        {
            Task.Factory
                .StartNew<IEnumerable<Foo>>(() => FindData(value))
                .ContinueWith(
                    task =>
                    {
                        this.foundData.Clear();
                        this.foundData.AddRange(task.Result);
                    },
                    TaskScheduler.FromCurrentSynchronizationContext());
        },
    
        /* ICommand.CanExecute */
        value => !String.IsNullOrWhitespace(value));
    

    Breaking this down into manageable parts, we’re starting a new task which calls some method IEnumerable<Foo> FindData(string). This is the plain old boring synchronous code you’ve always written. Likely it already exists on your view model!

    Next we tell the framework to start a new task when that one finishes using ContinueWith, but to do it on the WPF Dispatcher instead. This allows you to avoid the hassles of cross-thread problems with UI elements.

    You can extend this for monitoring with a helper class:

    public class TaskManager
    {
        private static ConcurrentDictionary<Dispatcher, TaskManager> _map
            = new ConcurrentDictionary<Dispatcher, TaskManager>();
    
        public ObservableCollection<WorkItem> Running
        {
            get;
            private set;
        }
    
        public TaskManager()
        {
            this.Running = new ObservableCollection<WorkItem>();
        }
    
        public static TaskManager Get(Dispatcher dispatcher)
        {
            return _map.GetOrAdd(dispatcher, new TaskManager());
        }
        // ...
    

    Using this class in XAML would be along the lines of adding its instance to your Window’s ViewModel:

    public TaskManager CurrentTaskManager
    {
        get { return TaskManager.Get(Dispatcher.CurrentDispatcher); }
    }
    // <StatusBarItem Content="{Binding CurrentTaskManager.Running.Count}" />
    

    You would then add a method to your TaskManager to handle the adding of tasks to and from the Running collection:

        public Task<TResult> StartNew<TResult>(Func<TResult> work)
        {
             var task = Task.Factory
                            .StartNew<TResult>(work);
    
             // build our view model
             var workItem = new WorkItem(task);
             this.Running.Add(workItem);
    
             // Pass the result back using ContinueWith
             return task.ContinueWith(
                 t => { this.Running.Remove(workItem); return t.Result; },
                 TaskScheduler.FromCurrentSynchronizationContext());
        }
    

    Now we simply change our FindDataCommand implementation:

    TaskManager.Get(Dispatcher.CurrentDispatcher)
               .StartNew<IEnumerable<Foo>>(() => FindData(value))
               .ContinueWith(
                   task =>
                   {
                       this.foundData.Clear();
                       this.foundData.AddRange(task.Result);
                   },
                   TaskScheduler.FromCurrentSynchronizationContext());
    

    The WorkItem class could expose the properties on the Task class to the UI, or it could be extended to encapsulate a CancellationToken to support cancellation in the future.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.