skip to Main Content

I have a Winforms app project in Visual Studio 2022 and every time I edit the code or the designer and try to run the app, I get this error:

Error MSB3021
Unable to copy file "X:School ProjectsHotel-Reservation-DemoHotel-ReservationHotel-ResobjDebugnet6.0-windowsapphost.exe" to "binDebugnet6.0-windowsHotel-Res.exe". The process cannot access the file ‘binDebugnet6.0-windowsHotel-Res.exe’ because it is being used by another process.
Hotel-Res X:ProgramVisual StudioMSBuildCurrentBinamd64Microsoft.Common.CurrentVersion.targets 5167

and:

Error MSB3027
Could not copy "X:School ProjectsHotel-Reservation-DemoHotel-ReservationHotel-ResobjDebugnet6.0-windowsapphost.exe" to "binDebugnet6.0-windowsHotel-Res.exe". Exceeded retry count of 10. Failed. The file is locked by: "Hotel-Res (22108)"
Hotel-Res X:ProgramVisual StudioMSBuildCurrentBinamd64Microsoft.Common.CurrentVersion.targets 5167

The only solution I found at this moment is to open the task manager and close "Hotel-Res" and then it run, but not every time I find it there. I tried on other desktops and the problem is there too.

2

Answers


  1. It sounds like you are trying to run a new instance of your program before stopping your current instance. This isn’t possible because you can’t overwrite the exe file while it’s being used.

    If you are wanting live updates while the program is running then you are probably looking for functionality of "hot reloading".

    Also worth noting that if my original sentence is incorrect, then you could be having policies or an antivirus on your device that scan newly created executable.

    Login or Signup to reply.
  2. The issue is that when Windows programs are running their EXE files become locked until the program unloads. This error is a sign that the program is not unloaded (is running), and of course you would need to terminate the program to unload it and free locks on the EXE file.

    Typically you would launch your application from within the IDE by pressing F5 or the Play icon to launch the application under the debugger. Once the application terminates, or you press the Stop icon, the application should unload and the EXE file should be released.

    Use the debugger to launch and shutdown the application and this should not be a problem. In some rare scenarios you may need to launch an application externally, but 99.999% of the time you should be launching under a debugger.

    If launching it manually, you need a way to exit the application between runs. For services this is usually a Service Control STOP request (sc.exe under Windows, systemctl under Linux, launchctl under macOS) — if a STOP request is not shutting down the application as expected it would most likely be caused by a bug in the program, such as spawning a thread without configuring it as a "background" thread (the ideal thread option), allowing it to hold the application open until all non-background threads exit.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search