gpt4 book ai didi

c# backgroundworker 无法使用我希望它执行的代码

转载 作者:行者123 更新时间:2023-11-30 19:32:44 25 4
gpt4 key购买 nike

我的代码在强制我尝试运行它时只得到一个错误时没有出现任何错误。它说 ThreadStateException 未被我在多个地方搜索过的用户代码处理,我的所有代码看起来都以我知道问题所在的相同方式工作。这是不起作用的代码

 private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
FolderBrowserDialog dlg2 = new FolderBrowserDialog();
if (dlg2.ShowDialog() == DialogResult.OK)
//do whatever with dlg.SelectedPath
{
DirectoryInfo source = new DirectoryInfo(dlg.SelectedPath);
DirectoryInfo target = new DirectoryInfo(dlg2.SelectedPath);

DirectoryInfo dir = new DirectoryInfo(dlg.SelectedPath);
FileInfo[] fis = dir.GetFiles("*", SearchOption.AllDirectories);
foreach (FileInfo fi in fis)
{
if (fi.LastWriteTime.Date == DateTime.Today.Date)
{
File.Copy(fi.FullName, target.FullName +"\\"+ fi.Name, true);
}
}

}
}

任何帮助将不胜感激

最佳答案

您无法通过线程显示表单(对话框)。

 private void button1_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog dlg2 = new FolderBrowserDialog())
{
if (dlg2.ShowDialog() == DialogResult.OK)
{
backgroundWorker1.RunWorkerAsync(dlg2SelectedPath);
}
}
}


private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string selectedpath = (string) e.Args;
....
}

此外,请确保您处理了 Completed 事件并检查 if (e.Error != null) ...
否则您将忽略错误。

关于c# backgroundworker 无法使用我希望它执行的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4899281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com