Media Element Keeping Files Open

Ken Krugh 116 Reputation points
2021-05-31T03:39:09.34+00:00

I have a media element next to a list of picture files. When the user clicks a picture in the list it displays in the Media Element.

When the user chooses to delete the file I'm setting the Media Element's source to nothing before displaying a confirmation dialog for the deletion, but the file can sometimes take 3 to 5 seconds before it can be deleted.

Is there something more I should be doing "free up" that file from the Media Element other than setting it's source to nothing?

Thanks!,
Ken

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,569 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
762 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ken Krugh 116 Reputation points
    2021-05-31T18:47:35.12+00:00

    Posting here because it was too long for a comment.

    I think things are pretty straight forward. The list is actually a data grid, but I don't think that has anything to do with the file being tied up. I'm using My.Computer.FileSystem.DeleteFile to delete the file. I've added a bunch of comments to, hopefully, make things clearer.

    I wrote the WaitIsFileFree routine when the file that was being displayed in the MediaElement wouldn't delete. It repeatedly tries to open the file with no sharing for the number of seconds passed (I'm currently using 5) before passing back that the file is NOT free. That routine seems to be working well and I can see in the Immediate windows how many times it fails to open the file. Sometimes its a bunch, sometimes it's none.

    The rest of the stuff after the "For Teach TheRow" loop is just managing things being removed.

    Thanks for having a look!

       Dim IsItFree As String
       'FreeFi holds the media element's srouce as a string before setting it to nothing.
       Dim FreeFi As String = Me.MedElePrev1.Source.ToString
       Me.MedElePrev1.Source = Nothing
       Try
           'This loop just collects the datagrid rows they have selected.
           For Each Cel In TheCurrSelCells
               Me.DGridFileList.SelectedItems.Add(Cel.Item)
           Next
           Dim DelMsg As String
           'Sets the user prompt to confirm the deletion. If it's only 1 file it shows the name, folder and date and time of the file.
           If Me.DGridFileList.SelectedItems.Count = 1 Then
               DelMsg = "This file will be deleted:" & vbCrLf & vbCrLf &
                        "File: " & TheCurrItem.FileName & vbCrLf &
                        "Folder: " & TheCurrItem.FolderName & vbCrLf &
                        "File Date/Time: " & FileDateTime(TheCurrItem.FileNameFull) & vbCrLf & vbCrLf &
                        "Continue?"
           Else
               DelMsg = Me.DGridFileList.SelectedItems.Count & " files will be deleted." & vbCrLf & vbCrLf &
                        "Continue?"
           End If
           'Prompt the user for confirmation of deletion.
           If MsgBox(DelMsg, vbOKCancel + vbQuestion, "Confrirm Delete") = vbCancel Then
               DoReSel = True
           Else
               'Change FreeFi from the URI used by the MediaElement to a local filename.
               FreeFi = New Uri(FreeFi).LocalPath
               AddHandler TheProgress.ProgressChanged, Sub(s, n)
                                                           Me.TxtInfo1.Text = n
                                                       End Sub
               'WaitIsFileFree returns values but am disregarding that here as we're just letting the delete attempt generate any errors.
               'Waits for the file to free up.
               Await Task.Delay(1000)
               IsItFree = Await WaitIsFileFree(FreeFi, 5, TheProgress)
               DoReSel = True     'set this because the cells may no longer be there
               DoRefresh = True
               'Goes through the selecdted items in the datagrid using the FileNameFull property of the underlying data for the filename to delete.
               For Each TheRow As PicMData In Me.DGridFileList.SelectedItems
                   'Attemepts to delete the file, this is where the access denied error happens when the file is tied up.
                   My.Computer.FileSystem.DeleteFile(TheRow.FileNameFull)
                   SelRows = Me.DGridFileList.ItemsSource
                   SelRows.Remove(TheRow)
               Next
               If TheCurrRowIdx > Me.DGridFileList.Items.Count - 1 Then TheCurrRowIdx = Me.DGridFileList.Items.Count - 1
               If Me.DGridFileList.Items.Count > 0 Then    'there are still some in the list
                   'reset the reselect stuff
                   Me.DGridFileList.SelectedIndex = TheCurrRowIdx
                   TheCurrItem = Me.DGridFileList.SelectedItem
                   Call TheCurrSelSet()
                   Me.DGridFileList.CurrentItem = TheCurrItem
               End If
           End If
       Catch ex As Exception
           Call UpdateErrField("Error deleting file: " & ex.Message, False, "true")
       End Try