gpt4 book ai didi

c# - 等到我的线程结束

转载 作者:太空宇宙 更新时间:2023-11-03 18:09:39 27 4
gpt4 key购买 nike

public delegate void FileEventHandler(string file);
public event FileEventHandler fileEvent;

public void getAllFiles(string path)
{
foreach (string item in Directory.GetDirectories(path))
{
try
{
getAllFiles(item);
}
catch (Exception)
{ }
}

foreach (string str in Directory.GetFiles(path, "*.pcap"))
{
// process my file and if this file format OK raised event to UI and add the file to my listbox
FileChecker fileChecker = new FileChecker();
string result = fileChecker.checkFIle(str);
if (result != null)
fileEvent(result);
}
}

private void btnAddDirInput_Click(object sender, EventArgs e)
{
ThreadStart ts = delegate { getAllFiles(pathToSearch); };
Thread thread = new Thread(ts);
thread.IsBackground = true;
thread.Start();
}

我想等到线程完成它的工作然后更新我的 UI

最佳答案

您可以使用任务并行库(而不是显式任务)以及异步语言功能来非常轻松地完成此任务:

private async void btnAddDirInput_Click(object sender, EventArgs e)
{
await Task.Run(() => getAllFiles(pathToSearch));
lable1.Text = "all done!";
}

关于c# - 等到我的线程结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18513205/

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