C # thread hangs in a closed file

I have a thread that calls a static method to update file properties using WindowsAPICodePack ShellPropertyWriter and BackgroundWorker. The thread calls the method below for every file in a folder of 1000+ files and hangs on ShellPropertyWriter.close () after the 700th update or so on.

Nothing to do with the file itself, tried using different files that were previously successfully updated.

    public static bool ShellPropertyUpdate(VideoEntry mediaEntry)
    {
        try
        {
            ShellFile mediafile = ShellFile.FromFilePath(mediaEntry.FilePath);
            ShellPropertyWriter pw = mediafile.Properties.GetPropertyWriter();
            pw.WriteProperty(SystemProperties.System.Music.Artist, mediaEntry.Actor);
            pw.WriteProperty(SystemProperties.System.Music.Genre, mediaEntry.Genre);
            pw.WriteProperty(SystemProperties.System.Rating, mediaEntry.Rating);
            pw.Close();
        }
        catch (Exception ex)
        {
           return false;
        }
        return true;
    }

    private void mnuWriteMetadataToFiles_Click(object sender, EventArgs ev)
    {
        this.WorkerThread = new BackgroundWorker();
        this.WorkerThread.DoWork += new DoWorkEventHandler(WorkerThread_WriteMetadataToFiles);
        this.WorkerThread.ProgressChanged += new ProgressChangedEventHandler(WorkerThread_ProgressChanged);
        this.WorkerThread.RunWorkerCompleted += (s, e) => WorkerThread_Completed("Writing metadata to files", s, e);
        this.WorkerThread.WorkerReportsProgress = true;
        this.WorkerThread.WorkerSupportsCancellation = true;
        this.WorkerThread.RunWorkerAsync(WMPlayer);
    }

    private void WorkerThread_WriteMetadataToFiles(object sender, DoWorkEventArgs e)
    {
        int counter = 0;
        BackgroundWorker worker = (BackgroundWorker)sender;
        MediaPlayer wmp = (MediaPlayer)e.Argument;

        // ... Loop with the foreach video in the library and write it to file.
        foreach (VideoEntry entry in wmp.Videos)
        {
            if (worker.CancellationPending)
            {
                e.Cancel = true;
            }
            else
            {
                worker.ReportProgress(counter, "Updating '" + entry.Filename + "'" + Environment.NewLine + "Processing file");
                if (VideoToFile.ShellPropertyUpdate(entry))
                {
                    result &= true;
                }
                counter++;
            }
        }
        e.Result = result;
    }

      

+3


source to share


2 answers


Never heard of this meeting before, but it smells like hand fatigue. Try this instead:

using (ShellFile mediafile = ShellFile.FromFilePath(mediaEntry.FilePath))
{
    ShellPropertyWriter pw = mediafile.Properties.GetPropertyWriter();
    pw.WriteProperty(SystemProperties.System.Music.Artist, mediaEntry.Actor);
    pw.WriteProperty(SystemProperties.System.Music.Genre, mediaEntry.Genre);
    pw.WriteProperty(SystemProperties.System.Rating, mediaEntry.Rating);
    pw.Close();
}

      



Here, each file descriptor is closed immediately, not at the discretion of the garbage collector. ShellFile

must implement IDisposable

for this to work, otherwise this code will not compile. I'm pretty sure I ShellFile

implement it.

+3


source


Apparently it has something to do with the files themselves. I pulled out several problem files and Thread continued processing until the next problem file. I don't know what happened to the file, however I am willing to submit updates to the problem files. Is there a way to stop / kill the thread? I cannot use DoWorkEventArgs.cancel () as the thread is hanging and not returning.



0


source







All Articles