Getting “cross-thread operation not valid” even wh

2019-07-17 02:11发布

问题:

I get the "cross-thread operation not valid" here:

if ( vlc.State == VlcPlayerControlState.PLAYING )
        {
            if ( vlc.InvokeRequired )
            {
                vlc.Invoke( new MediaPlayerNoParameterDelegate( vlc.Stop ) );
            }
            else
            {
                vlc.Stop(); // debugger points here
            }
        }

Debugging shows me that vlc doesn't require invoking. but the thread from which this is accessed is different from the thread it was created on.

I'm using the libvlc.net wrapper to play sound, but the problem should not be there. How can I get rid of this exception?

I'm using threading not backgroundworker.

Thank you!

回答1:

It sounds like this is a bug in the libvlc.net wrappers.

My suggestion would be to just always call vlc.Invoke(...). You'll suffer a (small) performance hit when the invoke is truly not required, but if you're doing this from a separate thread, it will always be required anyways.