Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with UI access from Background worker

I'm shifting a project over from winforms to WPF. When my code was based on WinForms I used (this.InvokeRequired) to check if the thread has access. Now I use the following code based on my Mainform :

    // this is the delegate declaration to Allow Background worker thread to write to Log Output
    delegate void LogUpdateCallBack(String LogMessage);

    // method to update the Log Window from the Background Thread
    public void LogUpdate(String LogMessage)
    {
        Console.WriteLine("Entering");
        if (!Application.Current.Dispatcher.CheckAccess())
        {
            Console.WriteLine("Thread doesn't have UI access");
            LogUpdateCallBack callback = new LogUpdateCallBack(LogUpdate);
            Application.Current.Dispatcher.Invoke(callback, LogMessage);
        }
        else
        {
            Console.WriteLine("Thread has UI access");
            listBox_Output.Items.Add(LogMessage);
            Console.WriteLine(LogMessage);
            // listBox_Output.TopIndex = listBox_Output.Items.Count - 1;
        }
        Console.WriteLine("Exiting");
    }

The issue I have is that the Listbox isn't updating. There are no errors or exceptions, I've tried updating other UI controls. The LogMessage is writing into the Output window so I'm stumped.

Here's sample Console output :

Entering
Thread doesn't have UI access
Entering
Thread has UI access
My LogMessage is output here 
Exiting
Exiting
Entering
Thread doesn't have UI access
Entering
Thread has UI access
My LogMessage is output here 
Exiting
Exiting

I've tried updating other UI controls just to check if it's an issue with my Listbox but with no luck.

Other than switching over to CheckAccess() the only other major change I've made in the new WPF code is to base all the code running in the Background worker in another Class .. I'm not sure if this could be part of the issue ?.

--

@JonRaynor

I tried your idea :

Application.Current.Dispatcher.BeginInvoke(new LogUpdateCallBack(LogUpdate), LogMessage)

However my Listbox still isn't updating, if I output

Console.WriteLine(listBox_Output);

I see the list box array growing :

System.Windows.Controls.ListBox Items.Count:2020
System.Windows.Controls.ListBox Items.Count:2021
System.Windows.Controls.ListBox Items.Count:2022
System.Windows.Controls.ListBox Items.Count:2023
System.Windows.Controls.ListBox Items.Count:2024
System.Windows.Controls.ListBox Items.Count:2025

But no change in the form. This is very confusing !.

like image 740
mrbencowell Avatar asked Jun 21 '11 20:06

mrbencowell


2 Answers

I just started on WPF as well and had to relearn from the old WinForms way. I've been using BeginInvoke() and this type of syntax on my screens (forms)...

public delegate void WorkCompleted();

        /// <summary>
        /// Marks the end of the progress
        /// </summary>
        private void ProgressComplete()
        {
            if (!this.Dispatcher.CheckAccess())
            {
                this.Dispatcher.BeginInvoke(new WorkCompleted(ProgressComplete), System.Windows.Threading.DispatcherPriority.Normal, null);
            }
            else
            {
                this.buttonClose.Visibility = Visibility.Visible;
                this.progressBarStatus.IsIndeterminate = false;
            }
        }
like image 185
Jon Raynor Avatar answered Sep 29 '22 09:09

Jon Raynor


I finally solved this.

My issue was that as I was calling the LogUpdate method from both another thread AND another class, therefore I needed to pass a reference to my main form which contained the listbox into this class rather than create a new instance of the main form in the secondary class.

so rather than have this declaration in my secondary class :

public MainWindow MainForm = new MainWindow();

I needed to pass a reference of the form into the method in the secondary class :

    public void Plot(BackgroundWorker worker, MainWindow MainForm)
like image 34
mrbencowell Avatar answered Sep 29 '22 10:09

mrbencowell