xamarin / XamarinCommunityToolkit

The Xamarin Community Toolkit is a collection of Animations, Behaviors, Converters, and Effects for mobile development with Xamarin.Forms. It simplifies and demonstrates common developer tasks building iOS, Android, and UWP apps with Xamarin.Forms.
MIT License
1.59k stars 472 forks source link

[Bug] MediaElement Event OnMediaEnded throw exception WRONG THREAD when put new path on Source media #1986

Closed behemothe closed 1 year ago

behemothe commented 1 year ago
When rise Event OnMediaEnded I want to put a new path on the Source of the media and this throw an exception WRONG THREAD ### Description

XAML : <toolkit:MediaElement x:Name="video" ShouldAutoPlay="True" ShouldShowPlaybackControls="True" HeightRequest="{OnPlatform 150, Android=50}" BackgroundColor="Black" IsVisible="true" MediaEnded="OnMediaEnded" />

Code: private void OnMediaEnded(object sender, EventArgs e) { PlayNextFichierMusic(); }

internal void PlayNextFichierMusic() { if (ecouter.NomFichierImage == "pause.png") { string path = ecouter.GetNextFichierMusic(RoutinesCommunes.SUFFIXE_ECOUTER_LA_MUSIC); video.Source = path; <---- Exception WRONG THREAD video.Play(); } }

Stack Trace

System.Runtime.InteropServices.COMException HResult=0x8001010E Message=L’application a appelé une interface qui était maintenue en ordre pour un thread différent. (0x8001010E (RPC_E_WRONG_THREAD)) Source=WinRT.Runtime Arborescence des appels de procédure : à WinRT.ExceptionHelpers.g__Throw|20_0(Int32 hr) à ABI.Microsoft.UI.Xaml.IUIElementMethods.InvalidateMeasure(IObjectReference _obj) à Microsoft.UI.Xaml.UIElement.InvalidateMeasure() à Microsoft.Maui.Platform.ViewExtensions.InvalidateMeasure(FrameworkElement platformView, IView view) à Microsoft.Maui.Handlers.ViewHandler.MapInvalidateMeasure(IViewHandler handler, IView view, Object args) à Microsoft.Maui.Handlers.ElementHandler.Invoke(String command, Object args) à Microsoft.Maui.Controls.VisualElement.InvalidateMeasureOverride() à Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IView.InvalidateMeasure() à Microsoft.Maui.Controls.VisualElement.InvalidateMeasureInternal(InvalidationTrigger trigger) à Microsoft.Maui.Controls.VisualElement.InvalidateMeasure() à CommunityToolkit.Maui.Views.MediaElement.OnSourcePropertyChanged(MediaSource newValue) à CommunityToolkit.Maui.Views.MediaElement.OnSourcePropertyChanged(BindableObject bindable, Object oldValue, Object newValue) à Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, SetterSpecificity specificity, Boolean silent) à Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value) à CommunityToolkit.Maui.Views.MediaElement.set_Source(MediaSource value) à MaMusique.Views.Ecouter.PlayNextFichierMusic() dans F:\Users\Behem\Sources\MaMusique MAUI 8.0\MaMusique MAUI 8.0\Views\Ecouter.xaml.cs :ligne 42 à MaMusique.Views.Ecouter.OnMediaEnded(Object sender, EventArgs e) dans F:\Users\Behem\Sources\MaMusique MAUI 8.0\MaMusique MAUI 8.0\Views\Ecouter.xaml.cs :ligne 126 à System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) à System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)

WinRT.Runtime.dll!WinRT.ExceptionHelpers.ThrowExceptionForHR.__Throw|20_0(int hr)   Inconnu
Microsoft.WinUI.dll!ABI.Microsoft.UI.Xaml.IUIElementMethods.InvalidateMeasure(WinRT.IObjectReference _obj)  Inconnu
Microsoft.WinUI.dll!Microsoft.UI.Xaml.UIElement.InvalidateMeasure() Inconnu
Microsoft.Maui.dll!Microsoft.Maui.Platform.ViewExtensions.InvalidateMeasure(Microsoft.UI.Xaml.FrameworkElement platformView, Microsoft.Maui.IView view) Inconnu
Microsoft.Maui.dll!Microsoft.Maui.Handlers.ViewHandler.MapInvalidateMeasure(Microsoft.Maui.IViewHandler handler, Microsoft.Maui.IView view, object args)    Inconnu
Microsoft.Maui.dll!Microsoft.Maui.Handlers.ElementHandler.Invoke(string command, object args)   Inconnu
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.VisualElement.InvalidateMeasureOverride()   Inconnu
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IView.InvalidateMeasure()  Inconnu
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.VisualElement.InvalidateMeasureInternal(Microsoft.Maui.Controls.Internals.InvalidationTrigger trigger)  Inconnu
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.VisualElement.InvalidateMeasure()   Inconnu
CommunityToolkit.Maui.MediaElement.dll!CommunityToolkit.Maui.Views.MediaElement.OnSourcePropertyChanged(CommunityToolkit.Maui.Views.MediaSource newValue)   Inconnu
CommunityToolkit.Maui.MediaElement.dll!CommunityToolkit.Maui.Views.MediaElement.OnSourcePropertyChanged(Microsoft.Maui.Controls.BindableObject bindable, object oldValue, object newValue)  Inconnu
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.BindableObject.SetValueActual(Microsoft.Maui.Controls.BindableProperty property, Microsoft.Maui.Controls.BindableObject.BindablePropertyContext context, object value, bool currentlyApplying, Microsoft.Maui.Controls.Internals.SetValueFlags attributes, Microsoft.Maui.Controls.SetterSpecificity specificity, bool silent)  Inconnu
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.BindableObject.SetValue(Microsoft.Maui.Controls.BindableProperty property, object value)    Inconnu
CommunityToolkit.Maui.MediaElement.dll!CommunityToolkit.Maui.Views.MediaElement.Source.set(CommunityToolkit.Maui.Views.MediaSource value)   Inconnu

MaMusique MAUI 8.0.dll!MaMusique.Views.Ecouter.PlayNextFichierMusic() Ligne 42 C# MaMusique MAUI 8.0.dll!MaMusique.Views.Ecouter.OnMediaEnded(object sender, System.EventArgs e) Ligne 126 C# [Transition Natif à Managé] [Transition Managé à Natif] System.Private.CoreLib.dll!System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(object obj, System.Span copyOfArgs, System.Reflection.BindingFlags invokeAttr) Inconnu System.Private.CoreLib.dll!System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] parameters, System.Globalization.CultureInfo culture) Inconnu Microsoft.Maui.dll!Microsoft.Maui.WeakEventManager.HandleEvent(object sender, object args, string eventName) Inconnu CommunityToolkit.Maui.MediaElement.dll!CommunityToolkit.Maui.Views.MediaElement.OnMediaEnded() Inconnu CommunityToolkit.Maui.MediaElement.dll!CommunityToolkit.Maui.Views.MediaElement.CommunityToolkit.Maui.Core.IMediaElement.MediaEnded() Inconnu CommunityToolkit.Maui.MediaElement.dll!CommunityToolkit.Maui.Core.Views.MediaManager.OnMediaElementMediaEnded(Windows.Media.Playback.MediaPlayer sender, object args) Inconnu Microsoft.Windows.SDK.NET.dll!WinRT._EventSource_globalWindows_Foundation_TypedEventHandler_global__Windows_Media_Playback_MediaPlayerobject_.EventState.GetEventInvoke.AnonymousMethod__1_0(Windows.Media.Playback.MediaPlayer sender, object args) Inconnu Microsoft.Windows.SDK.NET.dll!ABI.Windows.Foundation.TypedEventHandler<Windows.Media.Playback.MediaPlayer, object>.Do_Abi_Invoke<nint, nint>(void* thisPtr, nint sender, nint args) Inconnu

Link to Reproduction Sample

MaMusique MAUI 8.0.zip

Steps to Reproduce

  1. first click on 'Select' then click on '>' of '>ROOT' then select the MP4 file then click on 'OK Hear'
  2. click on Hear
  3. Click on round bouton to start the music
  4. At the end of the music, on MediaEnded, the exception appear

Expected Behavior

to have the next music

Actual Behavior

exception WRONG THREAD

Basic Information

  • Version with issue: the send zip file
  • Last known good version: none
  • IDE:
  • Microsoft Visual Studio Professional 2022 Version 17.8.0 Preview 2.0 VisualStudio.17.Preview/17.8.0-pre.2.0+34112.27 Microsoft .NET Framework Version 4.8.09032

Version installée : Professional

Visual C++ 2022 00476-80000-00000-AA245 Microsoft Visual C++ 2022

Adaptateur de test pour Boost.Test 1.0 Permet d'utiliser les outils de test de Visual Studio avec des tests unitaires écrits pour Boost.Test. Les conditions d'utilisation et les mentions tierces sont disponibles dans le répertoire d'installation de l'extension.

Adaptateur de test pour Google Test 1.0 Active l'utilisation des outils de test de Visual Studio avec les tests unitaires écrits pour Google Test. Les conditions d'utilisation et les notifications tierces sont disponibles dans le répertoire d'installation de l'extension.

ADL Tools Service Provider 1.0 This package contains services used by Data Lake tools

ASA Service Provider 1.0

ASP.NET and Web Tools 17.8.226.21692 ASP.NET and Web Tools

Azure Data Lake Tools for Visual Studio 2.6.5000.0 Microsoft Azure Data Lake Tools for Visual Studio

Azure Functions and Web Jobs Tools 17.8.226.21692 Azure Functions and Web Jobs Tools

Azure Stream Analytics Tools for Visual Studio 2.6.5000.0 Microsoft Azure Stream Analytics Tools for Visual Studio

Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Débogage d'image mémoire de noyau Linux 1.0.9.34112 Active le débogage des images mémoire de noyau Linux.

Extensibility Message Bus 1.4.39 (main@e8108eb) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

Extension VSPackage 1.0 Informations détaillées sur l'extension VSPackage Visual Studio

Gestionnaire de package NuGet 6.8.0 Gestionnaire de package NuGet dans Visual Studio. Pour plus d'informations sur NuGet, visitez https://docs.nuget.org/

Microsoft Azure Hive Query Language Service 2.6.5000.0 Language service for Hive query

Microsoft Azure Stream Analytics Language Service 2.6.5000.0 Language service for Azure Stream Analytics

Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Mono Debugging for Visual Studio 17.8.14 (0c9914e) Support for debugging Mono processes with Visual Studio.

Outils Azure App Service v3.0.0 17.8.226.21692 Outils Azure App Service v3.0.0

Outils C# 4.8.0-2.23429.7+44555193fd1135b5d53a2099f76fec91e0d1ebde Composants C# utilisés dans l'IDE. Selon votre type de projet et vos paramètres, une version différente du compilateur peut être utilisée.

Outils de développement Office pour Visual Studio 17.6.34024.01 Outils de développement Microsoft Office pour Visual Studio

Outils TypeScript 17.0.20830.2001 Outils TypeScript pour Microsoft Visual Studio

Outils Visual Basic 4.8.0-2.23429.7+44555193fd1135b5d53a2099f76fec91e0d1ebde Composants Visual Basic utilisés dans l'IDE. Selon votre type de projet et vos paramètres, une version différente du compilateur peut être utilisée.

PHP Tools for Visual Studio 1.74.18247.2022 PHP Tools extend Visual Studio with a set of features to build PHP applications more efficiently. It makes code more readable, easier to navigate, and clean.

Razor (ASP.NET Core) 17.8.2.2345506+ade90399d42c1a7bf92191b1c067816c0ae1c311 Fournit des services de langage pour ASP.NET Core Razor.

SQL Server Data Tools 17.8.64.0 Microsoft SQL Server Data Tools

Syntax Visualizer 1.0 An extension for visualizing Roslyn SyntaxTrees.

ToolWindowHostedEditor 1.0 Hosting json editor into a tool window

Visual C++ pour le développement mobile multiplateforme (Android) 15.0.34112.27 Visual C++ pour le développement mobile multiplateforme (Android)

Visual C++ pour le développement sous Linux 1.0.9.34112 Visual C++ pour le développement sous Linux

Visual F# Tools 17.8.0-beta.23425.10+0d3549fa5b8b6387ade191d76768405cefed8229 Microsoft Visual F# Tools

Visual Studio IntelliCode 2.2 Développement assisté par intelligence artificielle pour Visual Studio.

VisualStudio.DeviceLog 1.0 Informations sur mon package

VisualStudio.Mac 1.0 Mac Extension for Visual Studio

Workflow Manager Tools 1.0 1.0 This package contains the necessary Visual Studio integration components for Workflow Manager.

Xamarin 17.8.0.118 (main@35c256f) Extension Visual Studio permettant de développer pour Xamarin.iOS et Xamarin.Android.

Xamarin Designer 17.8.1.11 (remotes/origin/d17-8@13ef934098) Extension Visual Studio pour activer les outils Xamarin Designer dans Visual Studio.

Xamarin Templates 17.8.16 (830b56a) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK 13.2.1.2 (d17-5/a8a26c7) Xamarin.Android Reference Assemblies and MSBuild support. Mono: d9a6e87 Java.Interop: xamarin/java.interop/d17-5@149d70fe SQLite: xamarin/sqlite/3.40.1@68c69d8 Xamarin.Android Tools: xamarin/xamarin-android-tools/d17-5@ca1552d

Xamarin.iOS and Xamarin.Mac SDK 16.4.0.16 (b5972410d) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

  • Platform Target Frameworks:
    • iOS: no tested
    • Android: Its ok, it is working
    • Windows: Windows 11 last version
  • Android Support Library Version: no problem with Android
  • Nuget Packages:
  • Affected Devices:
  • MAUI Version:.NET 8.0

Workaround

No issue found

Reproduction imagery

screen exception

bijington commented 1 year ago

I think this belongs in the .NET MAUI Community Toolkit repo at: https://github.com/CommunityToolkit/Maui

But that being said have you tried using MainThread https://learn.microsoft.com/dotnet/maui/platform-integration/appmodel/main-thread to make sure that you are running on the correct thread?

behemothe commented 1 year ago

I think this belongs in the .NET MAUI Community Toolkit repo at: https://github.com/CommunityToolkit/Maui

But that being said have you tried using MainThread https://learn.microsoft.com/dotnet/maui/platform-integration/appmodel/main-thread to make sure that you are running on the correct thread?

Yes, think you very much, I resolve all my problem with MainThread.