SiamIT
Well-known member
- Joined
- Aug 3, 2021
- Messages
- 53
- Programming Experience
- 5-10
Greetings..
i am using following codes to run multiple threads at a time to get data from different links and save the data to different file as well. As the file name/path is different for each loop? i don't understand why it throws following exception:
here is my code:
Note: the exception throws at different loop each time..
best regards
i am using following codes to run multiple threads at a time to get data from different links and save the data to different file as well. As the file name/path is different for each loop? i don't understand why it throws following exception:
The process cannot access the file 'full file path' because it is being used by another process.
here is my code:
C#:
private async Task ProcessGameList(List<string> lRGameList, ExcelWorksheet oRSheet, int CurRowNum, string CurrentYear, int CurrentWeek, string FileInUse)
{
string sXML, sMessage, sFilePath;
List<Task> lTaskList;
Task tCurTask;
int LastProcessed;
lTaskList = new List<Task>();
LastProcessed = 0;
for (int iGameLoop = 0; iGameLoop < lRGameList.Count; iGameLoop++)
{
sFilePath = string.Format(@"{0}\Data\Game-{1}-{2}-{3}.xml", sAPPPath, CurrentYear, (CurrentWeek + 1), (iGameLoop + 1));
if (File.Exists(sFilePath))
{
continue;
}
tCurTask = new Task(() => CrawllGameData(lRGameList[iGameLoop], sFilePath));
tCurTask.Start();
lTaskList.Add(tCurTask);
if(lTaskList.Count == 7 || iGameLoop == (lRGameList.Count - 1))
{
sMessage = $"Downloading Game Data For Weeek {(CurrentWeek + 1)} Game {LastProcessed + 1} - {iGameLoop + 1}...";
this.Text = sMessage;
await Task.WhenAll(lTaskList.ToArray());
lTaskList.Clear();
LastProcessed = iGameLoop;
}
}
}
private void CrawllGameData(string URL, string sFilePath)
{
string sHTML, sXML;
//MessageBox.Show(URL);
sHTML = fGetHTML(URL);
File.WriteAllText(Path.ChangeExtension(sFilePath, ".html"), sHTML);
sXML = fHtml2Xml(sHTML);
File.WriteAllText(sFilePath, sXML);
}
Note: the exception throws at different loop each time..
best regards
Last edited: