The process cannot access the file because it is being used by another process.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
The process cannot access the file radar000207.Png because it is being used by another process.
The erorr is show up when i cancel the peration and then i want to delete the files from the temp directory and also to delete the preview file if they are exist.


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; private <span style="color:Blue; void button1_Click(<span style="color:Blue; object sender, EventArgs e)
{
<span style="color:Blue; if (backgroundWorker1.WorkerSupportsCancellation == <span style="color:Blue; true)
{
<span style="color:Green; // Cancel the asynchronous operation.
progressBar2.EndColor = Color.FromArgb(210, 0, 0);
label7.ForeColor = Color.Red;
label7.Text = <span style="color:#A31515; "Operation Has Been Cancelled";
timer1.Stop();
File.Delete(previewFileName);
<span style="color:Blue; foreach (FileInfo file <span style="color:Blue; in dir1.GetFiles())
{

file.Delete();
}
backgroundWorker1.CancelAsync();
}

}
[/code]
<br/>
In the doWork event in the loop for im doing:



<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; private <span style="color:Blue; void backGroundWorker1_DoWork(<span style="color:Blue; object sender, DoWorkEventArgs e)
{
BackgroundWorker bgw = (BackgroundWorker)sender;
List<<span style="color:Blue; string> Directories = (List<<span style="color:Blue; string>)e.Argument;

String radar_images_download_directory = Directories[0];
String tempRadarPngToGifDirectory = Directories[1];

List<<span style="color:Blue; string> myGifList = <span style="color:Blue; new List<<span style="color:Blue; string>();
dir1 = <span style="color:Blue; new DirectoryInfo(radar_images_download_directory);
pngFileInfo = dir1.GetFiles(<span style="color:#A31515; "*.png");
<span style="color:Blue; int total = pngFileInfo.Length;
<span style="color:Blue; int Counter = 0;
<span style="color:Blue; int percentage = 0;
<span style="color:Blue; for (<span style="color:Blue; int i = mtpStart; i < mtpEnd; i++)<span style="color:Green; //for (int i = mtpStart; i< mtpEnd;i++)//foreach (FileInfo SingleFileInfo in pngFileInfo)
{
<span style="color:Blue; if (bgw.CancellationPending == <span style="color:Blue; true)
{
e.Cancel = <span style="color:Blue; true;
<span style="color:Blue; break;
}
<span style="color:Blue; else
{
<span style="color:Blue; try
{
String FileName = tempRadarPngToGifDirectory + <span style="color:#A31515; "\" + Counter.ToString("D6<span style="color:#A31515; ") + ".Gif";

<span style="color:Green; // loading png and convert it to gif
Image gifImage = Image.FromFile(pngFileInfo.FullName);
gifImage.Save(FileName, System.Drawing.Imaging.ImageFormat.Gif);
Counter += 1;
<span style="color:Green; // add the filename to the List
myGifList.Add(FileName);
<span style="color:Green; // calculating percentage and report it
percentage = Counter * 100 / total;<span style="color:Green; //(total / 100) * Counter;
bgw.ReportProgress(percentage);

}
<span style="color:Blue; catch (Exception ex)
{
<span style="color:Green; // this is a bad idea inside a backgroundworker
MessageBox.Show(<span style="color:#A31515; "error >>>>>> " + ex);
}
}


}
numberOfFiles = myGifList.Count();
unfreez.MakeGIF(myGifList, previewFileName, 8, <span style="color:Blue; true);
bgw.ReportProgress(100);
<span style="color:Green; // whatever this will do

<span style="color:Green; //unfreez.MakeGIF(myGifList, previewFileName, animatedGifSpeed, loop);

<span style="color:Green; // setting the myGifList as the result of the method
e.Result = myGifList;

}
[/code]
<br/>
previewFileName is the animated gif i want to delete it if it exist when the user cancel the operation.

The dir1 is containing the files of the temp directory the files the user selected and that are added to list in the doWork for loop.
This is how i work with the dir1 in the constructor:


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public Animation_Radar_Preview()
{
InitializeComponent();
timer1.Interval = _blinkFrequency;
radar_images_download_directory = Options_DB.Get_Radar_Images_Download_Directory();
path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath);
tempRadarPngToGifDirectory = Path.Combine(path_exe, <span style="color:#A31515; "tempRadarPngToGifDirectory");
dir1 = <span style="color:Blue; new DirectoryInfo(tempRadarPngToGifDirectory);
<span style="color:Blue; foreach (FileInfo file <span style="color:Blue; in dir1.GetFiles())
{

file.Delete();
}
Directories = <span style="color:Blue; new List<<span style="color:Blue; string>();
Directories.Add(radar_images_download_directory);
Directories.Add(tempRadarPngToGifDirectory);
backgroundWorker1.RunWorkerAsync(Directories);
mtpStart = Picturebox1_Fullscreen.mtp1Start;
mtpEnd = Picturebox1_Fullscreen.mtp1End;
unfreez = <span style="color:Blue; new UnFreezWrapper();
previewDirectory = path_exe + <span style="color:#A31515; "\" + "previewDirectory";
<span style="color:Blue; if (Directory.Exists(previewDirectory))
{
}
<span style="color:Blue; else
{
Directory.CreateDirectory(previewDirectory);
}
previewFileName = previewDirectory + <span style="color:#A31515; "\" + "preview.gif";
numberOfFiles = mtpEnd - mtpStart;
label2.Text = numberOfFiles.ToString();
<span style="color:Green; //di = new DirectoryInfo(radar_images_download_directory);
<span style="color:Green; //fi = di.GetFiles("*.png");
<span style="color:Green; //nameOfStartFile = fi[mtpStart].Name.ToString();
<span style="color:Green; //nameOfEndFile = fi[mtpEnd].Name.ToString();

label6.Text = Picturebox1_Fullscreen.mtp1Start.ToString();<span style="color:Green; //nameOfStartFile.ToString();
label4.Text = Picturebox1_Fullscreen.mtp1End.ToString();<span style="color:Green; //myt1.y.ToString();//nameOfEndFile.ToString();
backgroundWorker1.WorkerSupportsCancellation = <span style="color:Blue; true;
}
[/code]
<br/>
If the user click cancel i want to delete this files.


Thanks. <hr class="sig danieli

View the full article
 
Back
Top