0

I have a big problem here and unable to fix it. I'm working on an update software at work who need to work even on a computer with running processes. So if a copy or other treatment cause exceptions, I need to kill the process that cause the error and retry and continue the update. The problem is I kill theses processes but windows continue to say to me that there is some processes opened even if I killed them.

So I have a method that copy files of the update on a computer where processes are running. I used Process.GetProcessesByName("processName") and solution below to kill processes.

Using C#, how does one figure out what process locked a file?

This is my code:

try
{
    file.CopyTo(destAbsolutePath, true);
}
catch (IOException ioException)
{
    foreach (var process in Process.GetProcessesByName(file.Name.Replace(Path.GetExtension(file.Name), "")))
    {
        process.Kill();
    }
    foreach (Process p in Win32Processes.GetProcessesLockingFile(file.FullName))
    {
        p.Kill();
    }
    file.CopyTo(destAbsolutePath, true);
}

I ask to me if Windows need time to know if a process is killed. I tried to wait a little bit but nothing happen. If I close my application after have killed these processes and reopen it, there is no more error. If I kill my application with GetCurrentProcess.Kill(), the error stay there. I don't think it's my application that cause the IOException because i'm only looping over FileInfo object.

I tried this solution to check if my file (a .dll custom file used by processes I killed) was locked after killing the processes who use my .dll file and it wan't locked:

Is there a way to check if a file is in use?

Please, help me. Thanks

Community
  • 1
  • 1

2 Answers2

0

You have a race condition I think. The Kill method exits asynchronously; call the WaitForExit method to wait for it to exit before you proceed. If these processes have a UI, you might also consider the CloseMainWindow method, it's a more "orderly" way of closing the program (but the call might fail - e.g. the program could refuse to close - so you have to check to see if the call succeeded).

  • I tried to call WaitForExit() but problem stay there. Finally, even if I kill my application and reopen it, I have the error. – Émile Pettersen-Coulombe Jul 27 '16 at 20:10
  • Oh and I retried 2-3 minutes after the last error to restart my program and... no more error... What should be the problem to this? – Émile Pettersen-Coulombe Jul 27 '16 at 20:15
  • Ok I found the problem but I need someone to help me to resolve it. At my work, there is simulators composed of several computers. I need to kill processes on all computers. So how to list all computers processes if I know that the computers are plugged and work together? – Émile Pettersen-Coulombe Jul 27 '16 at 20:45
0

Ok I found the problem but I need someone to help me to resolve it. At my work, there is simulators composed of several computers. I need to kill processes on all computers. So how to list all computers processes if I know that the computers are plugged and work together?

This means that a file share was accessed and keeping the file open. File share file handles are owned by the kernel. You cannot close them at all. The process finding logic does not work/apply here.

You need to make sure that the clients of the file share stop accessing the share. Alternatively, you could remove the share or kick all currently connected clients. I think removing the share is the best option because it is race condition free and it does not rely on killing something.

So how to list all computers processes if I know that the computers are plugged and work together?

That is a different question. This should be documented on the web already.

usr
  • 168,620
  • 35
  • 240
  • 369