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

  • Home
  • SEARCH
  • 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 5986509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:40:53+00:00 2026-05-22T22:40:53+00:00

I have a WPF (C# and .NET 4) application that has a long running

  • 0

I have a WPF (C# and .NET 4) application that has a long running task in it that blocked the UI giving the impression that it has hung. I decided to put this long-running task onto a separate thread by using a BackgroundWorker thread and showed a BusyIndicator in a separate popup window (named WorkingDialog below). This worked fine until the long running task writes to a BindingList (which is bound to a grid on the UI), and I got the following exception:

This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread

This is (a very trimmed down version of) the business layer code…

public class CustomMessage
{
    public DateTime TimeStamp { get; private set; }
    public string Message { get; private set; }

    public CustomMessage(string message)
    {
        Message = message;
        TimeStamp = DateTime.Now;
    }
}

public class MyRandomBusinessClass
{
    public BindingList<CustomMessage> LoggingList { get; private set; }

    public MyRandomBusinessClass()
    {
        LoggingList = new BindingList<CustomMessage>();
    }

    public void SomeLongRunningTask()
    {
        System.Threading.Thread.Sleep(5000);

        LoggingList.Add(new CustomMessage("Completed long task!"));
    }
}

… the UI …

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="350"
        Width="525">

    <StackPanel>
        <DataGrid x:Name="logGrid"
                  AutoGenerateColumns="True"
                  ItemsSource="{Binding Path=LoggingList}" />

        <Button Click="ProcessLongTask_Click"
                Content="Do Long Task" />
    </StackPanel>

</Window>

… The UI code …

using System.ComponentModel;
using System.Windows;

namespace WpfApplication4
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MyRandomBusinessClass RandomBusinessClass { get; set; }
        public MainWindow()
        {
            InitializeComponent();

            RandomBusinessClass = new MyRandomBusinessClass();

            logGrid.DataContext = RandomBusinessClass;
        }

        private void ProcessLongTask_Click(object sender, RoutedEventArgs e)
        {
            ProcessTask1();
        }

        private void ProcessTask1()
        {
            WorkingDialog workingDialog = new WorkingDialog();
            workingDialog.Owner = this;

            BackgroundWorker worker = new BackgroundWorker();

            worker.DoWork += delegate(object s, DoWorkEventArgs args)
            {
                RandomBusinessClass.SomeLongRunningTask();
            };

            worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
            {
                workingDialog.Close();
            };

            worker.RunWorkerAsync();
            workingDialog.ShowDialog();
        }
    }
}

… the worker dialog …

<Window xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
        x:Class="WpfApplication4.WorkingDialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        mc:Ignorable="d"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="Working Dialog"
        WindowStartupLocation="CenterOwner"
        Height="116"
        Width="199">

    <Grid>

        <extToolkit:BusyIndicator x:Name="busyIndicator"
                                  IsBusy="True"
                                  BusyContent="Working ..."
                                  HorizontalAlignment="Left"
                                  VerticalAlignment="Top" />

    </Grid>
</Window>

I’ve found several threads in this forum pointing to the work around by Bea Stollnitz), but I am wondering if there is a way of executing the long running task, showing the WorkingDialog and then closing the dialog once the task is complete using the Task Parallel Library? Can the Task Parallel Library achieve the desired result without having to modify the business layer code?

Edit – Workaround 1

Thanks to comments from Ben below, there is a workaround if your long-running task is in the same assembly as the UI or uses the System.Windows namespace. Using the current application dispatcher I can add the message to my list without the exception:

public class MyRandomBusinessClass
{
    public BindingList<CustomMessage> LoggingList { get; private set; }

    public MyRandomBusinessClass()
    {
        LoggingList = new BindingList<CustomMessage>();
    }

    public void SomeLongRunningTask()
    {
        System.Threading.Thread.Sleep(5000);

        Dispatcher myDispatcher = Application.Current.Dispatcher;
        myDispatcher.BeginInvoke((Action)(() => LoggingList.Add(new CustomMessage("Completed long task!"))));
    }
}

I’m still curious whether this would be possible using the Task Parallel Library or am I barking up the wrong tree?! In my actual application the long-running class is in a separate class library.

Edit 2

I’ve accepted Ben’s solution below as it solves my initial problem of getting the long-running task to run without blocking the UI or throwing the exception. I appreciate that the original question was about the Task Parallel Library, so forgive me if you are reading this thinking that it has a TPL solution.

  • 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-05-22T22:40:54+00:00Added an answer on May 22, 2026 at 10:40 pm

    You need to use a dispatcher to add a new element to your logginglist.

    public void SomeLongRunningTask()
    {
        System.Threading.Thread.Sleep(5000);
    
        Application.Current.Dispatcher.BeginInvoke((Action)(() => LoggingList.Add(new CustomMessage("Completed long task!"))));
    }
    

    Edit:

    public class MyRandomBusinessClass
    {
      public BindingList<CustomMessage> LoggingList { get; private set; }
      public Action<MyRandomBusinessClass, CustomMessage> CallBackAction { get; private set; }
    
      public MyRandomBusinessClass(Action<MyRandomBusinessClass, CustomMessage> cba) 
      {
          LoggingList = new BindingList<CustomMessage>();
          this.CallBackAction = cba;
      }
    
      public void SomeLongRunningTask()
      {
        System.Threading.Thread.Sleep(5000);
    
        if (cba != null)
          CallBackAction(this, new CustomMessage("Completed long task!"));
      }
    }
    

    Then where you create your business class:

    MyRandomBusinessClass businessClass = new MyRandomBusinessClass((sender, msg) =>
       Application.Current.Dispatcher.BeginInvoke((Action)(() => 
          sender.LoggingList.Add(msg))));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a WPF Application that has a two flavors with a consistent UI
I have a WPF .NET 3.5 SP1 application that is in use on at
I have .NET WPF application and one of requirement is that user may select
I have a WPF (.Net 3.5 sp1) application that loads a bunch of data
I have a WPF application with form that has a textbox named txtStatusWindow. I
I have a C# WPF (.NET 4.0) application that uses Excel interop to read
I have a C# (2008/.NET 3.5) class library assembly that supports WPF (based on
I have a WPF control, that has a list of Investors, and in the
I am building an application that has a WCF service that a WPF and
I'm an ASP.NET developer that has never done Winforms/WPF, but I need to create

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.