MediaElement not releasing the file on its Close() method in WPF
-
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
-
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
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.
-
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.
Thanks!!!
Praveen Raghuvanshi Software Developer