0

i just have a strange problem and i didn't get why it happens. Could you please help me to find out why it happens and how i can fix it?

My error is: "The Calling Thread Cannot Access This Object Because A Different Thread Owns It".

My point is that getting some values from databese (it takes a long time therefore i use Task.Run()) and creating custom UI Component with the values and add into a wrap panel.

Firstly, i have an event which is window loaded:

 private async void Window_Loaded(object sender, RoutedEventArgs e)
    {
        await this.ShowAllMatchesAsync(); // This code workfully works
    } // But after right here, it reports an error and doesn't show any code because it is in interrupt mode :(

I have a list which gets value by using async process:

private async Task ShowAllMatchesAsync()
{
      List<Match> allMatches = await new GetValues().GetAllMatchValuesAsync(0); // This workfully works. So, allMatches value has some values which i need it to create UI Using Control:

      // UI_MatchCard is a User Control which i have created 
      // This class needs a Match parameter, in this way it automatically creates MatchCard according to match value which i defined as parameter.
      UI_MatchCard matchCard = new UI_MatchCard(allMatches[0]); // I just want to show first match of allMatches for this case.

      // WP_Matches is a Wrap panel. I use it to add UI_MatchCard
      this.WP_Matches.Dispatcher.Invoke(() => { this.WP_Matches.Children.Add(matchCard); });
      // It workfully works here. I mean, it adds an UI_Matcherd to WP_Matches (this wrap panel) and count of Wp_Matches equals to 1 after adding it :)
}

When all process is done, it reports an error after processing where i told you (The last line of Windows_Loaded event).

The strange thing is that if I don't add an UI_MatchCard:

this.WP_Matches.Dispatcher.Invoke(() => { this.WP_Matches.Children.Add(matchCard); });

And if I add another UI component (like textblock)

this.WP_Matches.Dispatcher.Invoke(() => { this.WP_Matches.Children.Add(new TextBlock()); });

It doesn't report anything and workfully works. Also, if i add my UI_MatchCard without any parameter:

  UI_MatchCard matchCard = new UI_MatchCard(); // Without any parameter

  this.WP_Matches.Dispatcher.Invoke(() => { this.WP_Matches.Children.Add(matchCard); }); 

It workfully works again. But as you guess it doesn't show my match values. It just show blank match because i didn't enter the parameter.

Also these are what i have tried: Changing Invoke methodes:

 this.Dispatcher.Invoke(() => { this.WP_Matches.Children.Add(matchCard); });
 Application.Current.Dispatcher.Invoke(() => { this.WP_Matches.Children.Add(matchCard); });

And Task.ContinueWith() method.

How can i find out why it happens? Could you please help me?

  • Is there a BitmapSource or some other object with thread affinity in Match? – Clemens Jun 23 '21 at 12:51
  • Besides that, using `Dispatcher.Invoke` is pointless, as long as you call it from the UI thread, as you are doing here. – Clemens Jun 23 '21 at 12:52
  • @Clemens Yes there is. Can this be a problem ? – Yusufhaystra Jun 23 '21 at 13:23
  • Yes, that is the problem. In case it actually is a BitmapSource (which you did not tell us), you have to `Freeze()` it before you use it in the UI thread. – Clemens Jun 23 '21 at 13:24
  • @Clemens About Dispatcher.Invoke, you're completely right. Thank you for your answer – Yusufhaystra Jun 23 '21 at 13:28
  • @Clemens You are a hero. Thank you so so so so much. I understood what causes this error. When you have available time, could you please help me how can i fix it ? If (as you said) i freeze these ImageBrushes before using them in the UI thread, i can not set any ImageSource. I need these photos :( How can i do this?. Thank you for your helpful comments again. I really appreciate you. I have searched for 4 days and finally i could find what causes it thanks to you – Yusufhaystra Jun 23 '21 at 13:46
  • This may help: https://stackoverflow.com/a/35610774/1136211 – Clemens Jun 23 '21 at 13:56
  • I have completely fixed it. @Clemens thank you so so much again. I wish i will able to like you. Best wishes – Yusufhaystra Jun 23 '21 at 16:34

0 Answers0