I need to get the CurrentState
from a MediaElement
several times. I've created a simple function that looks like this:
private string getCurrentState(MediaElement m)
{
return m.CurrentState.ToString();
}
But everytime this function is called I get this:
An exception of type 'System.Exception' occurred in MyProject but was not handled in user code.
Additional information: The application called an interface that was marshalled for a different thread.
(Exception gfrom HRESULT: 0x8001010E (RCP_E_WRONG_THREAD))
I've been investigating about this issue and, as I've understood, it usually comes when trying to retrieve the CurrentState
before the MediaOpened
event's been fired. Actually, this not suits my case since this function is called after that. However I call the CurrentState
property I get the same exception and the weirdest thing of this all is that sometimes works, and sometimes not, so I definetely have got no idea of what is wrong in the code :S
Does anyone have any idea about how to fix it?
You are trying to get state from Non-ui thread. You should redesign you code to not interact with MediaElement from background thread (Task). Or you can wrap getCurrentState method invocation into Despatcher
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
<lambda for your code which should run on the UI thread>);
User contributions licensed under CC BY-SA 3.0