public partial class ImageCopier : Form {
private BackgroundWorker backgroundWorker = new BackgroundWorker();
public ImageCopier() {
InitializeComponent();
//backgroundWorker.DoWork += Copier_DoWork;
}
private void Form1_Load(object sender, EventArgs e) {
string[] accounts = Directory.GetDirectories("photolocation", "???").Select(d => new DirectoryInfo(d).Name).ToArray();
accountBox.Items.AddRange(accounts);
}
private void copyButton_Click(object sender, EventArgs e) {
if (accountBox.SelectedItems.Count == 0) {
MessageBox.Show("You must select an account.");
}
else {
for (int i = 0; i < accountBox.SelectedItems.Count; i++) {
MessageBox.Show("Starting on account " + accountBox.SelectedItems[i]);
for (int a = 0; a <= 9; a++) {
directoryLabel.Text = "In Folder " + a.ToString();
directoryLabel.Refresh();
string[] files = Directory.GetFiles("photolocation" + accountBox.SelectedItems[i] + "\photos\" + a + "\");
files = files.Where(f => f.Contains('$') != true).ToArray();
if (files.Count() != 0) {
for (int b = 0; b < files.Count(); b++) {
fileLabel.Text = "File " + (b + 1) + " of " + files.Count().ToString();
fileLabel.Refresh();
File.Copy(files[b], files[b].Replace("photolocation", "altlocation"), true);
}
}
else {
MessageBox.Show("Computer does not have read access to image server or there are no photos.");
}
}
MessageBox.Show("Finished account " + accountBox.SelectedItems[i]);
}
//backgroundWorker.RunWorkerAsync();
}
}
private void Copier_DoWork(object sender, DoWorkEventArgs e) {
for (int i = 0; i < accountBox.SelectedItems.Count; i++) {
MessageBox.Show("Starting on account " + accountBox.SelectedItems[i]);
for (int a = 0; a <= 9; a++) {
directoryLabel.Text = "In Folder " + a.ToString();
directoryLabel.Refresh();
string[] files = Directory.GetFiles("photolocation" + accountBox.SelectedItems[i] + "\photos\" + a + "\");
files = files.Where(f => f.Contains('$') != true).ToArray();
if (files.Count() != 0) {
for (int b = 0; b < files.Count(); b++) {
//fileLabel.Text = "File " + (b + 1) + " of " + files.Count().ToString();
//fileLabel.Refresh();
File.Copy(files[b], files[b].Replace("photolocation", "altlocation"), true);
}
}
else {
MessageBox.Show("Computer does not have read access to image server or there are no photos.");
}
}
MessageBox.Show("Finished account " + accountBox.SelectedItems[i]);
}
}
}
上面的代码是我调试时使用它的方式。我使用backgroundWorker和Copier_DoWork当它被构建时,因为它只是不响应。问题是,当我以这种方式运行它时,它会在几个小时内遍历所有文件并复制它们。考虑到它要复制多少图像,这是相当快的。但是,如果使用后台工作器,它只能拖动,我估计复制相同数量的文件可能需要35个小时。
我更改了这段代码的文件夹位置名称,但是"photolocation"one_answers"altlocation"是两个不同的网络计算机和需要复制照片的位置。大多数图像都相对较小,最常见的是120kb。时不时地用一个随机的大点的。
这并不是一个大问题,因为我只是把这个问题放在一起,将在本周后由其他代码修复,但我仍然很好奇是什么导致它慢了这么多。
上面的代码没有多大意义…例如,Copier_DoWork
直接访问UI线程来更新控件,你甚至可以做一些显式刷新。整个方法都是错误的,所以不要期望有好的结果!
另外,如果您的处理时间很长,您应该报告您的进度并显示进度条。你可以使用额外的参数发送数据给UI线程,比如你想在UI上更新的文本。
如果您在调试时打开包括MDA在内的所有异常,您可能会更好地了解问题发生的位置。Release
版本比Debug
版本慢,这是非常不寻常的。
或者,你以某种方式对配置做了不希望的修改,或者在一个非常奇怪的情况下。
同样,在执行后台处理时显示消息框也没有任何意义。如果这是为了调试目的,那么在发布问题之前删除它,如果你甚至没有谈论你的问题中显示的消息。如果这是你实际的UI,那么你应该认真地重新考虑它。
我最终通过使用FileStreams作为开始位置和结束位置以及CopyTo从backgroundWorker中的第一个流到第二个流而不是File来修复它。复制和它的工作速度快得多,就像文件一样快。