KRyTeX
KRyTeX

Reputation: 63

How to access Listbox from another Thread UWP

I want to log into a listbox in uwp from another thread.

I know from windows forms that i should use delegates. But their are no invoke methods. This is my interface i use for logging. And that was the first thing i tried to use.

public interface ILoggingService
{
    void LogInformation(LogEntryType logEntryType,string logEntryMessage);
}



delegate void LoggerDelegate(LogEntryType logEntryType,string message);
LoggerDelegate _loggerDelegate = new LoggerDelegate(LogInformation);

public void LogInformation(LogEntryType logEntryType,string logEntryMessage)
    {
        if (lbxInformation.InvokeRequired)
        {
           lbxRequestInformation.Invoke(logRequestInformationDelegate,message);
        }
        else
        {
            lbxInformation.Items.Add(message);
        }
    }

Upvotes: 0

Views: 158

Answers (1)

Stefan Wick  MSFT
Stefan Wick MSFT

Reputation: 13850

Use Dispatcher.RunAsync to update the UI from another thread. Here is the documentation:

https://learn.microsoft.com/en-us/windows/uwp/threading-async/using-windows-runtime-objects-in-a-multithreaded-environment

Upvotes: 1

Related Questions