Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9164621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:43:38+00:00 2026-06-17T14:43:38+00:00

Context: I am playing music through a media element, and using a slider to

  • 0

Context: I am playing music through a media element, and using a slider to display the point in the song that it is at. That updating is done in a backgroundworker, for obvious reasons.

private void bgPlay_DoWork(object sender,DoWorkEventArgs e)
    {

        while (isMediaPlaying)
        {
            this.Dispatcher.Invoke((Action)(() =>
            {
                timelineSlider.Value = mediaElement1.Position.TotalMilliseconds;
            }));

        }
    }

    private void Library_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        try
        {
            mediaElement1.Stop();
            isMediaPlaying = false;
            mediaElement1.Source = new Uri(songData[Library.SelectedIndex].Location);
            mediaElement1.Volume = (double)volumeSlider.Value;

            mediaElement1.Play();

            isMediaPlaying = true;

            bgPlay.RunWorkerAsync();
        }
        catch(Exception ex) {
            F.MessageBox.Show(ex.ToString());
        }
    }

When I play a song, then double click on a different one, the background worker is still looping and throws an exception because it reaches bgPlay.RunWorkerAsync(); before the previous instance has finished. I tried to use the isMediaPlaying bool to tell the backgroundworker when to exit the loop, but the main thread reaches bgPlay.RunWorkerAsync(); before it finishes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:43:39+00:00Added an answer on June 17, 2026 at 2:43 pm

    You are suffering of a common mistake when one is barely starting to program with threading, a race condition

    I’d advise rewriting the code like this:

    private static String threadingLock = "";
    
    private void bgPlay_DoWork(object sender,DoWorkEventArgs e)
        {
    
            while (true)
            {
                lock(threadingLock) {
                    if(!isMediaPlaying)
                        break;
                }
                this.Dispatcher.Invoke((Action)(() =>
                {
                    timelineSlider.Value = mediaElement1.Position.TotalMilliseconds;
                }));
    
            }
        }
    
        private void Library_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                lock(threadingLock) {
                    isMediaPlaying = false;
                }
                mediaElement1.Stop();
    
                mediaElement1.Source = new Uri(songData[Library.SelectedIndex].Location);
                mediaElement1.Volume = (double)volumeSlider.Value;
    
                mediaElement1.Play();
    
                isMediaPlaying = true;
    
                bgPlay.RunWorkerAsync();
            }
            catch(Exception ex) {
                F.MessageBox.Show(ex.ToString());
            }
        }
    

    As a friendly tip, add a Thread.sleep(200) before invoking the update on the slider. It will reduce cpu usage without affecting the functionality of your application.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my app, I am using media player in service for playing a music
I'm playing with a grails app that has a contextmenu (on right-click). The context
Playing with a Mobile application in ASP.NET MVC4 beta, using great that article ,
I am trying to pause music that is playing when the headset is unplugged.
i am using this code for playing music for my website way2enjoy.com .it playes
I want to display different context-sensitive content during a video playing on a website.
When any music player playing a song, clicked on its notification it will show
I'm open to suggestions. I'm writing an app that needs a music playing between
I have this app with C# Code but the MP3 just not playing. music
i have tried to play music via spinner and i have done it. Whenever

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.