When renaming file in Backgroundworker, file is "being used by another process"
<p>The majority of C# SO questions about this error involve Streamwriter. I don't believe this question is related.</p>

<p>I have a WinForms application. The app spawns a backgroundworker to do the real work (image processing) to stay responsive. In one function, I attempt to rename all image files in a directory (without opening them). I receive the error "<strong>The process cannot access the file because it is being used by another process.</strong>" I can't track down the source of the error.</p>

<p>In MainForm (form) class - spawn the backgroundworker:</p>

<pre><code>private void b_preprocessImages_Click(object sender, EventArgs e)
    {
        if (bgw_preprocessor.IsBusy == false)
        {
            bgw_preprocessor.RunWorkerAsync();
        }
        else 
        {...}
    }
</code></pre>

<p>Still in MainForm class - DoWork method for backgroundworker:</p>

<pre><code>private void bgw_preprocessor_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        ImagePreprocessor preprocessor = new ImagePreprocessor();
        preprocessor.SetImageDirectoryPath(path);
        preprocessor.PreprocessImages(worker);    
    }
</code></pre>

<p>Inside ImagePreprocessor class - method that the backgroundworker runs:</p>

<pre><code>public void PreprocessImages(BackgroundWorker worker)
    {
        this.worker = worker;

        if (!Directory.Exists(SourceImageDirectoryPath))
            return;
        else
        {...}

        if (!AreAllImagesValid())
            return;
        else
        {...}

        print.PrintLine("Renaming files...");
        AutoRenameAllImages();
</code></pre>

<p>Inside ImagePreprocessor class - method to rename images:</p>

<pre><code>public void AutoRenameAllImages()
    {
        for (int i = 0; i &lt; sourceImageFiles.Length; i++)
        {
            // sourceImageFiles[] is an array of strings
            FileInfo f = new FileInfo(sourceImageFiles[i]);
            string newName = Path.Combine(SourceImageDirectoryPath, "src_" + i.ToString() + f.Extension);
            print.PrintLine("Renaming " + f.Name + " to " + (new FileInfo(newName)).Name);

            try
            {
                f.MoveTo(newName);
                //File.Move(sourceImageFiles[i], newName);   // doesn't work either
            }
            catch (Exception ex)
            {
                ...
            }
        }
    }
</code></pre>

<hr>

<p>The error is immediately encountered upon trying to rename the first file. As far as I can tell, none of the files in question are open in any other applications. How can I fix this?</p>