multithreading - 线程和使用webclient中的for循环问题
问题描述
我有一个 csv 文件。在第 41 列中找到用“;”分隔的 url。我做了一个程序,它遍历每一行,然后用“;”分隔单元格[41] 到数组,然后遍历这个数组并将每个 url 下载到一个文件 jpg. 因为它是一个大数据,所以我用线程来做。我的问题是,for 循环不是以 1 开头,而是以 82 或 84 开头,并且跳过了很多行。你能帮我么?
代码:启动线程:
private void BtnDownloadPictures_Click(object sender, EventArgs e)
{
dstFile = "newFile.csv";
lblStatus.Text = "Starting...";
ThreadStart threadStart = dp;
Thread thread = new Thread(threadStart);
thread.Start();
}
private void dp()
{
// calculating how many rows there is in csv file
var file = new StreamReader(dstFile).ReadToEnd();
var lines = file.Split(new char[] { '\n' });
var count = lines.Length;
label3.SafeInvoke(d => d.Text = "Number of rows : " + count.ToString());
progressBar1.SafeInvoke(d => d.Maximum = 100);
progressBar1.SafeInvoke(d => d.Minimum = 0);
// here begins the for loop. It should start with 1. But that doesn’t happen
for (var i = 1; i < count - 1; i++)
{
var cells = lines[i].Split(',');
var urls = cells[41].Split(';');
for (var j = 0; j < urls.Length - 1; j++)
{
txtStatus.SafeInvoke(d => d.Text += i.ToString() + ": " + urls[j] + Environment.NewLine);
if (urls[j].StartsWith("http"))
{
lblStatus.SafeInvoke(d => d.Text = "Downloading picture number " + j.ToString() + " from row " + i.ToString());
downloadPics(urls[j]);
}
}
var p = (int)Math.Round((double)(100 * i) / count);
progressBar1.SafeInvoke(d =>
{
d.Value = p;
d.Refresh();
d.CreateGraphics().DrawString(p.ToString() + "%",
new Font("Arial", (float)8.25, FontStyle.Regular),
Brushes.Black,
new PointF(d.Width / 2 - 10, d.Height / 2 - 7));
});
}
txtStatus.SafeInvoke(d => d.Text += "Mission finisched successfully" + Environment.NewLine);
txtStatus.SafeInvoke(d => d.SelectionStart = d.Text.Length);
txtStatus.SafeInvoke(d => d.SelectionLength = 0);
}
private void downloadPics(string url)
{
try
{
string cd = Directory.GetCurrentDirectory() + @"\downloaded pictures";
string fn = cd + @"\" + url.Substring(url.LastIndexOf('/') + 1);
if (!Directory.Exists(cd))
{
Directory.CreateDirectory(cd);
}
if (!File.Exists(fn))
{
WebClient wc = new WebClientWithTimeout();
wc.DownloadFile(url, cd + @"\" + url.Substring(url.LastIndexOf('/') + 1));
txtStatus.SafeInvoke(d => d.Text += "Added successfully! " + Environment.NewLine);
}
else
{
txtStatus.SafeInvoke(d => d.Text += "File already exists " + Environment.NewLine);
}
}
catch (WebException we)
{
// add some kind of error processing
txtStatus.SafeInvoke(d => d.Text += we.Message + Environment.NewLine);
}
finally
{
txtStatus.SafeInvoke(d => d.SelectionStart = d.Text.Length);
txtStatus.SafeInvoke(d => d.SelectionLength = 0);
}
}
解决方案
我发现了问题。Cell[41] 并不总是包含 url。这意味着 csv 文件中的错误。对不起,我想删除这个问题
推荐阅读
- reactjs - 当两件事发生变化时反应触发 useEffect
- c# - 通过通用列表
使用 ViewBag 从 Controller 到 View 的数据 - python - ValueError:'z' 是 pandas 0.23.4 而不是 0.24.2 中格式为 '%Y-%m-%dT%H:%M:%S.%f%z' 的错误指令?
- c - 在 C 中释放本地定义的变量
- c# - 无法加载 Excel 工作表 Visual Studio 2019
- json - 从 esp8266 (arduinoIDE) 上的 json 获取长值时出现奇怪的错误
- laravel - 在我进入刀片之前我可以在控制器中做数学吗
- python - 写入 excel 文件时超过 url 的数量
- c# - 带有 ItemTemplate 的 WPF 可搜索组合框
- python - Flask-Sqlalchemy 动态绑定