问题
(Relating to an earlier question of mine: Playing a Video (MSDN Tutorial))
I have the following problem: When I'm trying to read the state of a XNA VideoPlayer object I get an CrossThreadMessagingException
. Meaning, I'm trying to read from a different thread. This seems to be not allowed (I have no idea how threads work in C# internally :-)
Any ideas on how to fix this? (Is there a way to tell the VideoPlayer object to write in a buffer or something?)
Code:
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
if (player.State == MediaState.Stopped) //Causes the exception
{
player.IsLooped = true;
player.Play(video); //Causes the "mysterious" crash
}
// ...
}
Error Message:
player.State = 'player.State' threw an
exception of type
'Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException'
回答1:
Yeah well it's apparently not just this him having the issue as I have the exact same problem , works fine on my computer, port to the 360 and "BANG" "CRASH" "BOOM". I'm working on it in other forums and found this while searching for an answer.
Jason
Sylven Game Development
回答2:
That's strange, because the Update method is on the main thread. It suggests to me that the video player was loaded, or is being updated on another thread ... can you update the question with details on how you're loading/initializing the video player?
回答3:
Try putting the player = new VideoPlayer(); in to the update method (check that you haven't created it already etc).
Does this error ever occur when not running in the debugger?
Also, your code's not checking that the loading of the video content has been completed before you start trying to play it - try delaying when the video playing starts and see if this reduces the issue.
回答4:
I have experienced this problem, and can say with some certainty that you are viewing this error whilst debugging (stepping through the code). This is in fact expected behaviour as the debugger itself is trying to access from a different thread (unfortunately I can't find the source I read that supported this), but your actual code will be able to access the property fine.
The actual error is very likely to be something else, such as an invalid movie file (see here for video requirements: http://msdn.microsoft.com/en-us/library/dd254869.aspx).
来源:https://stackoverflow.com/questions/5875020/xna-videoplayer-object-causes-crossthreadmessagingexception