Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. MediaElement not releasing the file on its Close() method in WPF

MediaElement not releasing the file on its Close() method in WPF

Scheduled Pinned Locked Moved WPF
csharpwpfdesignperformancehelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Praveen Raghuvanshi
    wrote on last edited by
    #1

    Hello, I need to play multiple instances of a same media file on a system and delete the file whenever last instance is deleted/closed. There is no UI involved in it and media is played/stopped on some triggers. I have been able to play the file using MediaElement and I am able to listen to same song in parallel and stop them individually. The issue occured while deletion of the media file. MediaElement doesn't release the filestream on MediaElement.Close() and the file is not deleted. It gets deleted only after application is closed. I feel it to be a timing issue between MediaElement.Close() and File.Delete() as it happens intermittently. I wanted to delete the file in order to create memory by deleting unused file(s). It can be reporduced by creating atleast 2 MediaElement whose Source(Uri) refer to same media file. After playing both the MediaElement, we close the media and delete the file(s). Sometimes it deletes the file, sometime doesn't. Appreciate any thoughts on this issue of file being locked by the MediaElement and unable to delete instantly. Thanks in advance!

    Praveen Raghuvanshi Software Developer

    L 1 Reply Last reply
    0
    • P Praveen Raghuvanshi

      Hello, I need to play multiple instances of a same media file on a system and delete the file whenever last instance is deleted/closed. There is no UI involved in it and media is played/stopped on some triggers. I have been able to play the file using MediaElement and I am able to listen to same song in parallel and stop them individually. The issue occured while deletion of the media file. MediaElement doesn't release the filestream on MediaElement.Close() and the file is not deleted. It gets deleted only after application is closed. I feel it to be a timing issue between MediaElement.Close() and File.Delete() as it happens intermittently. I wanted to delete the file in order to create memory by deleting unused file(s). It can be reporduced by creating atleast 2 MediaElement whose Source(Uri) refer to same media file. After playing both the MediaElement, we close the media and delete the file(s). Sometimes it deletes the file, sometime doesn't. Appreciate any thoughts on this issue of file being locked by the MediaElement and unable to delete instantly. Thanks in advance!

      Praveen Raghuvanshi Software Developer

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Yes; it sounds like a "timing" issue (since it happens "sometimes"). Some things (like I/O) can happen asynchronously. I would "queue" the name of the file and delete it on a separate thread; in a limited "retry" loop that sleeps a few milliseconds between tries. Run a timer / counter to see the best and worst case to satisfy your curiosity.

      P 1 Reply Last reply
      0
      • L Lost User

        Yes; it sounds like a "timing" issue (since it happens "sometimes"). Some things (like I/O) can happen asynchronously. I would "queue" the name of the file and delete it on a separate thread; in a limited "retry" loop that sleeps a few milliseconds between tries. Run a timer / counter to see the best and worst case to satisfy your curiosity.

        P Offline
        P Offline
        Praveen Raghuvanshi
        wrote on last edited by
        #3

        Thanks!!!

        Praveen Raghuvanshi Software Developer

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups