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:
delegate void LogUpdateCallBack(String LogMessage);
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);
}
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
. !