gpt4 book ai didi

c# - 进程无法访问该文件,因为它正被 Directory.Move() 上的另一个进程使用

转载 作者:太空狗 更新时间:2023-10-30 01:08:39 40 4
gpt4 key购买 nike

我们有一个具有以下架构的客户端应用程序:一个管理器进程管理几个工作进程(读取器和写入器)并定期向服务器查询版本更新。如果版本更新可用,管理器将其下载到客户端计算机,关闭工作线程,启动更新进程来处理更新并退出。更新程序在启动时接收管理器 PID 和更新文件位置;然后等待manager退出,备份manager和worker的所有文件,重新创建他们的目录,并将新版本文件传播到新目录。

当按照描述完成这个过程时,第一次调用 Directory.Move(string, string) - 用于备份管理器目录 - 抛出 IOException .奇怪的是,如果我让管理器关闭而不启动更新程序,然后自己启动更新程序可执行文件,则不会抛出异常。

管理工作线程的管理器代码:

public void Run()
{
_config = GetConfiguration();
Process reader, writer;

//Start reader and writer with appropriate arguments

//Keep reader and writer alive

reader.Kill();
writer.Kill();

reader.WaitForExit();
writer.WaitForExit();

reader.Dispose();
writer.Dispose();
}

查询数据库的管理器代码:

EndpointAddress endpoint;
BasicHttpBinding httpBinding = new BasicHttpBinding();
httpBinding.MaxReceivedMessageSize = 2000000000;
ChannelFactory<IService> chanFactory = new ChannelFactory<IService>(httpBinding);
IService service;

try
{
endpoint = new EndpointAddress(ConfigurationManager.AppSettings["Service URL"]);

service = chanFactory.CreateChannel(endpoint);

UpdateInstructions instructions = service.GetUpdateInstructions(_config.SiteID, Assembly.GetExecutingAssembly().GetName().Version.ToString(), _config.Version);

HandleUpdateInstructions(instructions); //Downloads files and starts the updater process
}
catch (Exception ex)
{
//Report exception
}
finally
{
if (chanFactory.State != CommunicationState.Faulted)
chanFactory.Close();
}

启动更新进程的管理器代码:

private void StartUpdater(string updateFilePath, string configFilePath)
{
ProcessStartInfo updaterStartInfo = new ProcessStartInfo(_config.UpdaterExePath, string.Format("{0} \"{1}\" \"{2}\"", Process.GetCurrentProcess().Id, updateFilePath, configFilePath));
Process updater = Process.Start(updaterStartInfo);
updater.Dispose();
}

等待管理器关闭的更新程序代码:

bool isManagerUp = true;

while (isManagerUp)
{
try
{
Process managerProcess = Process.GetProcessById(bDoxForceManagerPID);

managerProcess.WaitForExit();

managerProcess.Dispose();

isManagerUp = false;
}
catch
{
isManagerUp = false;
}
}

更新模块的更新程序代码:

//updateDirectory is the directory of the new files to be inserted, moduleDirectory is the working directory of the module that will be updated, in this case the manager
private void UpdateModule(DirectoryInfo updateDirectory, DirectoryInfo moduleDirectory)
{
string backupDirectory = MakeBackupDirectoryFullPath(moduleDirectory.Parent.FullName);

Directory.Move(moduleDirectory.FullName, backupDirectory); // IOException as described above.
Directory.CreateDirectory(moduleDirectory.FullName);

foreach (FileInfo updateFile in updateDirectory.EnumerateFiles())
{
string newFilePath = moduleDirectory.FullName + "\\" + updateFile.Name;

File.Copy(updateFile.FullName, newFilePath);
}

Directory.Delete(updateDirectory.FullName, true);
}

最佳答案

感谢Adam Caviness回答我们能够弄清楚。

我们的进程是控制台应用程序,它们创建了一个 .vshost 文件,这些文件在进程被命令终止后继续工作。尝试移动包含正在运行的 .vshost 文件的目录导致了问题。

将进程转换为 Windows 服务不会创建 .vshost 文件并解决了这个问题。

关于c# - 进程无法访问该文件,因为它正被 Directory.Move() 上的另一个进程使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8879405/

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