views:

313

answers:

1

My application does this, play the selected sound using wmplib and upon statechangeevent = 1 (media player stopped), i will close the player using the close() command and setting the URL to "";

Now every time, i tried to delete a sound that is currently playing, in actual fact, it will be deleted, because when I looked at the directory, the file has been deleted. Every time after I delete a file, I will reload the controls which will list be the media file within the directory. However, it will still find the sound that is already been deleted in the directory.

I have tried this way, debugging slowly, and I found out that the File info delete() command takes a longer time to delete, being the fact that closing the media player takes a longer time for it to delete, thats why whenever I try reloading the collections after deleting a currently playing song, the deleted song is still there. When I tried to delete when the media player is not playing the song, it deletes fast and the reloading collections did show the result of the song being gone.

The question is, how can I delete the file that is currently being played, ensuring that the reloading collections will show the correct number of song in the directory?

Any help would be greatly appreciated.

My code is as below.

 DirectoryInfo di = new DirectoryInfo(@"C:/Sounds/Custom Sounds/");
        FileInfo[] filepaths = MultipleFileFilter(di, "*.mp3,*.wma,*.wav");
        for (int i = 0; i < filepaths.Length; i++)
        {
            if (Path.GetFileNameWithoutExtension(Convert.ToString(filepaths[i]))== deleteItem)
            {

                System.IO.FileInfo fi = new System.IO.FileInfo(@"C:/Sounds/Custom Sounds/" + Convert.ToString(filepaths[i]));
                fi.Delete();
            }
        }

// Load the sounds
        DirectoryInfo dii = new DirectoryInfo(@"C:/Sounds/Custom Sounds/");
        FileInfo[] filess = MultipleFileFilter(dii, "*.mp3,*.wma,*.wav");
        for (int i = 0; i < filess.Length; i++)
        {
            RadioButton rb = new RadioButton();
            rb.Text = Path.GetFileNameWithoutExtension(filess[i].ToString());
            rb.Name = Path.GetFileName(filess[i].ToString());
            rb.CheckedChanged += new EventHandler(radioButton_Update);
            Point lc = new Point(15, yPos);
            rb.Location = lc;
            panelSound.Controls.Add(rb);
            lstbxCustomSounds.Items.Add(Path.GetFileNameWithoutExtension(rb.Text));

            yPos += 20;
            //add into the arraylist
            controlsCollections.Add(rb);
        }

Maybe this is the problem? Because only the currently playing sound that is being deleted will cause this problem?

private void wPlayer_PlayStateChange(int newstate)
    {

        if (newstate == 1 )//Media finished
        {
            btnPlay.Text = "Play";
            wplayer.close();
            btnDelete.Enabled = true;
            wplayer.URL = "";
        }
        else if (newstate == 3)
        {
            btnDelete.Enabled = false;
        }
A: 

It seems to be that an easy answer to this problem would be to not allow the deletion of something that is currently being played...either that or ensure that the delete process is complete before reloading the collection.

EDIT Based off your code above, when you're instantiating the fileinfo that is to be deleted, you're appending Convert.ToString(filepath[i]) to the filepath of the new fileinfo object. Instead, I'd get rid of everything before that and just use filepath[i].FullName.

So I'd just instantiate the object to be deleted as new FileInfo(filepath[i].FullName);.

Aaron
So how can I allow ensure that the delete process is completed before reloading the collection? I tried a while loop (checking the directory for the filename), it hanged the program like for 5 secs before reloading the collections.
Daniel
When are you performing the delete? Is it on another thread? I say update the collection right after the delete statement. Regardless of how long the delete statement takes, it'll still update after the delete is finished.
Aaron
The delete is not in another thread. My reload collection works this way, traverse through the directory and find the files within the directory. After I delete the file, I will call my reload collection method and sadly, they can still find the file there. I was thinking that the file is not fully being deleted yet, thats why it is still able to sense that file.
Daniel
Can you post some code? If you call your reload function after the delete statement has finished, then the directory should not contain the deleted item.
Aaron
Have added some codes in the question.
Daniel
Even with my edited statement, the problem must still lie with how and when you're refreshing your list of files...if the delete takes place, then it would certainly be reflected if you're refreshing your list after the deletion takes place. That is, unless, they're occurring on different threads.
Aaron
I've figured out maybe it is the media player problem because only the current playing song will be deleted will cause the problem. If the deleted item is not being played before or after it the delete key is pressed, there wont be any problem. I've attached the media player state change event handler.
Daniel
I'd definitely suggest that the file currently being played cannot be deleted
Aaron
I have already done that. That's why i disable the delete button. However immediately after the file is played finish or stopped, they will be able to delete the file, which cause the problem now.
Daniel
Perhaps add if(File.Exists(filess[i].FullName)) before adding to your list...perhaps it really is deleted, but the directoryinfo is not properly refreshing?
Aaron
I tried adding the condition, it worked. However, I tried again another time, with a different song, it doesnt work. THis is very queer. >.< How can i check whether it is properly being refreshed?
Daniel
Gotta love stuff like this...there is a Refresh() method for directory info. Perhaps try di.Refresh() after deleting your file...I don't see how that can help since you're instantiating a new directoryinfo to redraw your list, but at this point, anything is possible...
Aaron
Edited: previous comment, doesnt work, tried 5 times with the file, still same problem.
Daniel
I cant find a way out for this, meanwhile, I'll just put a thread.sleep(5000), before the delete button is enabled. It is the player problem, i strongly feel though.
Daniel