Problem accessing user interface from background worker

I am transferring a project from winforms to WPF. When my code was based on WinForms, I used (this.InvokeRequired) to check if the stream 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 problem is that Listbox is not updating. There are no errors or exceptions, I tried updating other interface elements. LogMessage writes to the Exit window, so I'm at a dead end.

Here is an example of 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 tried updating other user interface controls to check if there was a problem with my list, but with no luck.

CheckAccess(), , WPF, - , . , ?.

-

@JonRaynor

:

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

- ,

Console.WriteLine(listBox_Output);

:

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

. !

+5
3

, , .

, LogUpdate , , , .

:

public MainWindow MainForm = new MainWindow();

:

    public void Plot(BackgroundWorker worker, MainWindow MainForm)
+1

WPF WinForms. BeginInvoke() ()...

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;
            }
        }
+2

WPF DispatcherObject. , ListBox , . , . ( LogMessage), :

public void LogUpdate(string LogMessage)
{
    Console.WriteLine("Entering");
    if (listBox_Output.Dispatcher.CheckAccess())
    {
        listBox_Output.Dispatcher.Invoke(new LogUpdateCallBack(delegate
        {
            listBox_Output.Items.Add(LogMessage);
        }));
        Console.WriteLine(LogMessage);
    }
}
0

All Articles