I have one process, in that process it has to delete files from the folder. While deleting the files custom progress form is opening and showing the deleted status in percent. This is working fine. But if I clicked/double clicked on parent form or progress form application is getting freeze and progress form stop to updating the status(It is in Label). The process of delete is running behind but status is not updating on progress form. This application is in vs2005.
Below is the code.
private void DeleteFiles()
{
string path = "c:\Example";
if (Directory.Exists(path ))
{
Cursor = Cursors.No;
long deletedSize = 0;
frmLoading frmProgress = new frmLoading();
frmProgress.ShowInTaskbar = false;
frmProgress.lblLoading.Text = "Deleting Files ...(0%)";
frmProgress.Show(frmMain);
frmProgress.Refresh();
frmMain.Refresh();
long totalSize = DirSize(new DirectoryInfo(path));
EmptyFolder(new DirectoryInfo(path), totalSize, frmProgress, ref deletedSize);
Directory.Delete(path, true);
frmProgress.Close();
Cursor = Cursors.Default;
}
}
private void EmptyFolder(DirectoryInfo directoryInfo, long totalSize, frmLoading frmProgress ,ref long deletedSize)
{
short percent;
//long deletedSize=0;
try
{
foreach (FileInfo file in directoryInfo.GetFiles())
{
try
{
deletedSize += file.Length;
file.Delete();
percent = Convert.ToInt16((deletedSize * 100) / totalSize);
frmProgress.lblLoading.Text = "Deleting Files ...(" + percent.ToString() + "%)"; // Here progress form label is updating.
frmProgress.Refresh();
}
catch (Exception ex)
{
}
}
foreach (DirectoryInfo subfolder in directoryInfo.GetDirectories())
{
EmptyFolder(subfolder, totalSize,frmProgress,ref deletedSize);
}
}
catch (Exception ex)
{
}
}
Your help may appreciated.
Thanks