我正在为我的游戏制作修补程序。修补程序使用用户计算机上的本地文件检查ftp服务器上每个文件的大小,如果不同,请从ftp服务器下载新文件。如何检查递归函数是否完成?
我想添加一条说明“更新完成!X文件更新”的打印消息。当它检查完所有文件但我知道函数何时结束?
private void CheckForUpdates(string ftpPath, string localPath)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpPath);
request.Credentials = new NetworkCredential(ftpUser, ftpPass);
request.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
List<string> files = new List<string>();
string line = reader.ReadLine();
while (!string.IsNullOrEmpty(line))
{
if (line != "." && line != ".." && line != ".htaccess")
{
files.Add(line);
}
line = reader.ReadLine();
}
response.Close();
responseStream.Close();
reader.Close();
foreach (string file in files)
{
string newFtpPath = ftpPath + "/" + file;
string newLocalPath = localPath + "/" + file;
new Thread(() => AppendText("Checking " + newLocalPath + Environment.NewLine)).Start();
if (IsFile(newFtpPath))
{
if (File.Exists(newLocalPath))
{
if (!IsFileEqual(newFtpPath, newLocalPath))
{
new Thread(() => AppendText(file + " is out of date or corrupted. Patching in progress..." + Environment.NewLine)).Start();
new Thread(() => Download(newFtpPath, newLocalPath)).Start();
}
}
else
{
new Thread(() => AppendText(file + " is missing. Patching in progress..." + Environment.NewLine)).Start();
new Thread(() => Download(newFtpPath, newLocalPath)).Start();
}
}
else
{
if (!Directory.Exists(newLocalPath))
{
new Thread(() => AppendText(file + " is missing. Patching in progress..." + Environment.NewLine)).Start();
Directory.CreateDirectory(newLocalPath);
}
new Thread(() => CheckForUpdates(newFtpPath, newLocalPath)).Start();
}
}
}
第一次调用该方法后,您将会知道。所以在第一次调用递归函数之后执行下一行。我通常把静态计数器,以确保我不会无限递归开始(当我测试) –
你在这里的问题我看到的是,你是从新线程产卵。我肯定会跟踪你正在创建多少个线程,并使用诸如信号量之类的东西来控制它。 –
想过这个之后。我不一定会在线程中使用递归,它附带的各种问题都相当复杂。我只是使用标志和一个while循环来安排需要再次检查更新。或者,也许重新考虑要求 –