I have the following code which gets some information from a google sheets which runs async:

```
private async void getCodes()
    {
        ArrayList codes = await Task.Run(() =>
        {
            return GoogleAPI.GetDates();
        });
        foreach (DateTime date in codes)
        {
            Debug.WriteLine("**************");
            Debug.WriteLine(date);
            codesList.Items.Add("Test");
        }
    }

```

The `codesList.Items.Add("Test");` line crashes the application as its then trying to run the foreach on the background thread rather than the main thread. The writelines work fine.

> 
> 'The application called an interface that was marshalled for a different thread.
> 
> 
> 

Research shows I should be able to use:

```
await Window.Current.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>

```

To run it on the main thread, but this crashes with:

> 
> System.NullReferenceException: 'Object reference not set to an instance of an object.'Microsoft.UI.Xaml.Window.Current.get returned null
> 
> 
> 

Which according to the documentation is correct for a desktop app. If it makes a difference I'm using project reunion