question

KenKrugh-6537 avatar image
0 Votes"
KenKrugh-6537 asked DaisyTian-1203 commented

Media Element Keeping Files Open

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

dotnet-visual-basicdotnet-wpf-xaml
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@KenKrugh-6537
How did you delete the file? Could you show me the related code of your picture list and deleting? Please show me more info to reprodure your time delay issues to analyze.

0 Votes 0 ·

1 Answer

KenKrugh-6537 avatar image
0 Votes"
KenKrugh-6537 answered DaisyTian-1203 commented

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
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Are FreeFi (which is freed) and TheRow.FileNameFull (which is deleted) the same things?



0 Votes 0 ·

When just one file is being processed, yes, they are, FreeFi is redundant in a way.

I could likely use the FileNameFull of a selected item instead of assigning to FreeFi, but given the number of ways the user could have things selected the simplest way to ensure I know which file the MediaElement is using is to get it's source filename.

0 Votes 0 ·

@KenKrugh-6537
Could you show more details about My.Computer.FileSystem.DeleteFile? Which collection did you use to host player list? Is it List<T>? If it is, could you try to use Remove to delete the file?


0 Votes 0 ·