the code below works fine let's say i have a three files to download the first 2 is 2MB and the third is 12MB it downloads all the files correctly but after downloading the first 2 files it fires downloadfilecompleted event but after the third file it doesn't fire.
if i try to move this file or doing anything to it >> it's used by another process
and it never completes until i stop the debugging
after i stop debugging i look at the third file it's downloaded correctly
so i dunno why this event is not firing after downloading this file
i used breakpoints through all code and it seems that there's no exception is thrown
but the scenario here is the thread is frozen after downloading this third file.
Note: this file is not invalid file or corrupted file it's fine i downloaded it with browser,download tools,etc it's fine and the application also downloads it fine.
my code :
#region private fields
private bool isFinished = false;
private List<DownloadFileInfo> downloadFileList = null;
private List<DownloadFileInfo> allFileList = null;
private ManualResetEvent evtDownload = null;
private ManualResetEvent evtPerDonwload = null;
private static WebClient clientDownload;
long total = 0;
long nDownloadedTotal = 0;
#endregion
#region The ConStructor of DownloadProgress
public DownloadProgress(List<DownloadFileInfo> downloadFileListTemp)
{
PureEye.DailyOperation.FrmMain.UpdateStatus = 1;
InitializeComponent();
this.downloadFileList = downloadFileListTemp;
allFileList = new List<DownloadFileInfo>();
foreach (DownloadFileInfo file in downloadFileListTemp)
{
allFileList.Add(file);
}
}
#endregion
private void OnFormLoad(object sender, EventArgs e)
{
evtDownload = new ManualResetEvent(true);
evtDownload.Reset();
ThreadPool.QueueUserWorkItem(new WaitCallback(this.ProcDownload));
}
private void ProcDownload(object o)
{
string tempFolderPath = Path.Combine(CommonUnitity.SystemBinUrl, ConstFile.TEMPFOLDERNAME);
if (!Directory.Exists(tempFolderPath))
{
Directory.CreateDirectory(tempFolderPath);
}
evtPerDonwload = new ManualResetEvent(false);
long totalfilesize = 0;
foreach (DownloadFileInfo file in this.downloadFileList)
{
total += file.Size;
}
try
{
while (!evtDownload.WaitOne(0, false))
{
if (this.downloadFileList.Count == 0)
break;
DownloadFileInfo file = this.downloadFileList[0];
totalfilesize = file.Size;
this.ShowCurrentDownloadFileName(file.FileName);
clientDownload = new WebClient();
NetworkCredential objCred = new NetworkCredential();
objCred.UserName = ftpUserID;
objCred.Password = ftpPassword;
clientDownload.Proxy.Credentials = objCred;
clientDownload.Credentials = objCred;
clientDownload.DownloadProgressChanged += (object sender, DownloadProgressChangedEventArgs e) =>
{
try
{
int filePercentage = (int)(e.BytesReceived * 100 / totalfilesize);
int totalPercentage = (int)((nDownloadedTotal + e.BytesReceived) * 100 / total);
this.SetProcessBar(filePercentage, totalPercentage);
}
catch
{
}
};
clientDownload.DownloadFileCompleted += (object sender, AsyncCompletedEventArgs e) =>
{
try
{
DealWithDownloadErrors();
DownloadFileInfo dfile = e.UserState as DownloadFileInfo;
nDownloadedTotal += dfile.Size;
this.SetProcessBar(0, (int)(nDownloadedTotal * 100 / total));
evtPerDonwload.Set();
}
catch (Exception)
{
}
};
evtPerDonwload.Reset();
string tempFolderPath1 = CommonUnitity.GetFolderUrl(file);
if (!string.IsNullOrEmpty(tempFolderPath1))
{
tempFolderPath = Path.Combine(CommonUnitity.SystemBinUrl, ConstFile.TEMPFOLDERNAME);
tempFolderPath += tempFolderPath1;
}
else
{
tempFolderPath = Path.Combine(CommonUnitity.SystemBinUrl, ConstFile.TEMPFOLDERNAME);
}
clientDownload.DownloadFileAsync(new Uri(file.DownloadUrl), Path.Combine(tempFolderPath, file.FileFullName), file);
evtPerDonwload.WaitOne();
clientDownload.Dispose();
clientDownload = null;
this.downloadFileList.Remove(file);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
ShowErrorAndRestartApplication();
}
if (downloadFileList.Count > 0)
{
return;
}
DealWithDownloadErrors();
foreach (DownloadFileInfo file in this.allFileList)
{
string tempUrlPath = CommonUnitity.GetFolderUrl(file);
string oldPath = string.Empty;
string newPath = string.Empty;
try
{
if (!string.IsNullOrEmpty(tempUrlPath))
{
oldPath = Path.Combine(CommonUnitity.SystemBinUrl + tempUrlPath.Substring(1), file.FileName);
newPath = Path.Combine(CommonUnitity.SystemBinUrl + ConstFile.TEMPFOLDERNAME + tempUrlPath, file.FileName);
}
else
{
oldPath = Path.Combine(CommonUnitity.SystemBinUrl, file.FileName);
newPath = Path.Combine(CommonUnitity.SystemBinUrl + ConstFile.TEMPFOLDERNAME, file.FileName);
}
System.IO.FileInfo f = new FileInfo(newPath);
if (!file.Size.ToString().Equals(f.Length.ToString()) && !file.FileName.ToString().EndsWith(".xml"))
{
ShowErrorAndRestartApplication();
}
string newfilepath = string.Empty;
if (newPath.Substring(newPath.LastIndexOf(".") + 1).Equals(ConstFile.CONFIGFILEKEY))
{
if (System.IO.File.Exists(newPath))
{
if (newPath.EndsWith("_"))
{
newfilepath = newPath;
newPath = newPath.Substring(0, newPath.Length - 1);
oldPath = oldPath.Substring(0, oldPath.Length - 1);
}
File.Move(newfilepath, newPath);
}
}
if (File.Exists(oldPath))
{
MoveFolderToOld(oldPath, newPath);
}
else
{
if (!string.IsNullOrEmpty(tempUrlPath))
{
if (!Directory.Exists(CommonUnitity.SystemBinUrl + tempUrlPath.Substring(1)))
{
Directory.CreateDirectory(CommonUnitity.SystemBinUrl + tempUrlPath.Substring(1));
MoveFolderToOld(oldPath, newPath);
}
else
{
MoveFolderToOld(oldPath, newPath);
}
}
else
{
MoveFolderToOld(oldPath, newPath);
}
}
}
catch (Exception exp)
{
}
}
this.allFileList.Clear();
if (this.downloadFileList.Count == 0)
Exit(true);
else
Exit(false);
evtDownload.Set();
}