Home
Options

DumpPanel STA exception

edited January 17

In what I hope is the last step in the DumpPanel saga (for my purposes, at least).

Reference code

Running the code, I get the exception

for line

when attempting to write to the second panel. The first is created and displayed correctly.

For some background, I was initially getting this problem to happen on the first iteration, but somehow rewrote the code enough to avoid this issue. Even more background, my initial work at dumping to separate panels didn't use a browser control, but created a Label control and used StackWpfElementAsync to add it to the panel. From there, I obtained the bounding ScrollViewer via GetWpfElement and cached Dispatcher for the entire run. The Label (start) was created with Invoke if a Dispatcher was cached. This worked, but had other side-effects (like not allowing selection of the output text in the panel).

  if (dispatcher is null)
    start = new Label { Content = "Starting conversion for " + title, Foreground = new SolidColorBrush(Color.FromRgb(220, 220, 220)), Padding = new Thickness(1, 1, 1, 1), FontSize = 13 };
  else
    dispatcher.Invoke(() => start = new Label { Content = "Starting conversion for " + title, Foreground = new SolidColorBrush(Color.FromRgb(220, 220, 220)), Padding = new Thickness(1, 1, 1, 1), FontSize = 13 });

  var op = await PanelManager.StackWpfElementAsync(start, title);
  var wrapper = op.GetWpfElement() as ScrollViewer;
  dispatcher = wrapper.Dispatcher;
  dispatcher.Invoke(() => wrapper.Background = new SolidColorBrush(Color.FromRgb(28, 28, 28)));

I'd rather not have to fallback to caching the Dispatcher as this code will eventually reside in My Extensions. What am I missing here?

Comments

  • Options

    You can avoid this error by adding the following line of code to the start of your query:

    Util.CreateSynchronizationContext();
    

    This creates a synchronization context on the main thread, which ensures that subsequent awaits return to the same thread.

  • Options
    edited January 18

    Sadly, adding this seems to cause problems in other async/await code. Specifically, that other code appears "hung".

    The code in question:

        var seasons = details
          .Seasons
          .Select(async s => await GetTvSeason(client, first.Id, s.SeasonNumber))
          .Select(s => s.Result);
    

    This result is referenced inside a foreach loop later in the code. It was stuck on .Select(s => s.Result) (lazy load of the LINQ results). It even hung after adding .ToList() after the final Select. It took changing the code to loop through each Season, calling to get the data, and adding that data to a previously declared list. Then I could loop through the list without further issue.

  • Options

    Does adding .ConfigureAwait(false) to the GetTvSeason method call in this scenario help?

Sign In or Register to comment.