0

I try to create a thread and manipulate UI controls inside it, then I recived an exception System.Runtime.InteropServices.COMException: 'The application called an interface that was marshalled for a different thread.
Through searching, I found this is a very common problem, here is one way I have found

Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
    {
        // Your UI update code goes here!
    }
);

But this cause another exception
System.InvalidOperationException: 'A method was called at an unexpected time.

I don't know the correct way

private void MessageReceived(string message_content)
{
    InvertedListView.Items.Add(new Message(message_content, DateTime.Now,HorizontalAlignment.Left));
}

static bool flag = true;

Thread thread = new Thread(() =>
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = "C:\\Users";
    watcher.Filter = "text.txt";
    watcher.NotifyFilter = NotifyFilters.LastWrite;
    watcher.Changed += OnChanged;
    Debug.WriteLine("START!");
    watcher.EnableRaisingEvents = true;

    void OnChanged(object sender, FileSystemEventArgs e)
    {
        if (flag)
        {
            Debug.WriteLine("OnChanged");
            Thread.Sleep(10);
            var lastLine = File.ReadLines(watcher.Path + watcher.Filter, Encoding.UTF8).Last();
            MessageReceived(lastline); // The exception happened here
            Debug.WriteLine(lastLine);
            flag = false;
        }
        else
        {
            flag = true;
        }
    }
});
Wu Xiuheng
  • 53
  • 5
  • just use `BindableObject.Dispatcher.Dispatch(...)` .... of course you need instance of `BindableObject` but ... this is almost any control ... [some more info](https://stackoverflow.com/questions/74801284/for-dotnet-maui-what-the-difference-between-application-current-dispatcher-dis) – Selvin Aug 09 '23 at 10:02
  • Could you explain in more detail, where should I place it? – Wu Xiuheng Aug 09 '23 at 10:07

1 Answers1

3

To create/change an UI control, you need to do it on the UI thread. Let me give you a simple example:

private DispatcherQueue _dispatcherQueue;

public MainPage()
{
    this.InitializeComponent();
    this.Loaded += MainPage_Loaded;
    _dispatcherQueue = DispatcherQueue.GetForCurrentThread();
}

private async void MainPage_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
    // This works because this event handler is called on the UI thread.
    Button button = new();

    // This throws an exception because the button is not created on the UI thread.
    await Task.Run(() =>
    {
        Button button = new();
    });

    // This works because the DispatcherQueue is used to create the button on the UI thread.
    this.DispatcherQueue.TryEnqueue(() =>
    {
        Button button = new();
    });

    // This works because the DispatcherQueue is used to create the button on the UI thread.
    _dispatcherQueue.TryEnqueue(() =>
    {
        Button button = new();
    });
}
Andrew KeepCoding
  • 7,040
  • 2
  • 14
  • 21
  • or simply `this.Dispatcher.Dispatch(...)` ... since `this` (`MainPage`) is derived from `Page` and `Page` is derived from `BindableObject` ... – Selvin Aug 09 '23 at 10:24
  • 1
    `Dispatcher.Dispatch(...)` and `BindableObject` exits in WPF but not in WinUI 3. According to a comment from a member of the WinUI 3 team, `DispatcherQueue.TryEnqueue(...)` is the recommended way. – Andrew KeepCoding Aug 09 '23 at 11:00