V
V
Viktor Zorin2021-02-18 22:41:38
WPF
Viktor Zorin, 2021-02-18 22:41:38

How to access MediaPlayer.Position from timer handler?

There is a piece of the following code:

private System.Timers.Timer updatePosTimer = new System.Timers.Timer(250);

        public MediaController() {
            mediaPlayer = new MediaPlayer();
            updatePosTimer.Elapsed += (object s, ElapsedEventArgs e) => HandleUpdatePosTimer();
        }
        private void HandleUpdatePosTimer()
        {
            
            if (Convert.ToInt32(mediaPlayer.Position.TotalSeconds) != Convert.ToInt32(SongPosition.TotalSeconds))
            {
                SongPosition = mediaPlayer.Position;
            }
            
        }


Each tick timer should check if the Position of the object of the MediaPlayer type has changed. But when trying to get the value of the Position property, it throws an exception with the following content: "The calling thread cannot access this object because another thread owns this object."

Please help me understand

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-02-18
@FutureAnge1

Dispatcher will allow you to execute a piece of code on the UI thread

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question