Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locking files when building in Visual Studio 2010

Hello there, Stackoverflow.

Recently, when I've been programming in Visual Studio 2010, I've been getting the problem with VS locking the bin/Debug/(ProjectName).exe file when trying to build and gives me the error below after trying to build the project 10 times:

Unable to copy file "obj\x86\Debug\TileEngine.exe" to "bin\x86\Debug\TileEngine.exe". The process cannot access the file 'bin\x86\Debug\TileEngine.exe' becuase it is being used by another process.

The problem appears when I edit the source and then try to Debug. I've checked using different programs, and the only program using the file is Visual Studio.

If I wait for about 10 minutes before trying to build, it seems to work properly, but when trying different things, it isn't good needing to wait 10 minutes before trying something.

I've tried different solutions both on this site as well as everywhere I can find on Google.

Some solutions I've found, but haven't worked for me

Solution 1 - Using a pre-build script

In some different questions here on Stackoverflow, I've found one solution being that you go into Project Properties > Build Events and then in the Pre-build event command line add:

if exist "$(TargetPath).locked" del "$(TargetPath).locked"
if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"

This made it possible for me to build the project one more time than I usually could, but when editing the code again, and then building, the same error appeared.

Note: Trying to build a release instead of a debug build seems to break the pre-build script and it exits with the code '1', which seems to make VS unable to build properly. Removing the pre-build script makes it work like "normal" again, still with the same error though.

Solution 2 - Running Visual Studio as Administrator

This is another solution I've found, but havent worked either for me, so I assume that Visual Studio already have all the permissions required and running as Administrator doesn't actually make any difference.

Solution 3 - Changing the AssemblyVersion

In this question, Visual Studio build fails: unable to copy exe-file from obj\debug to bin\debug, I found another solution that included changing the AssemblyVersion, in the Properties\AssemblyInfo.cs file, to "2.0.0.0". This, however, haven't made any difference whatsoever for me.

Solution 4 - Closing UserControl designers before building

According to some different answers here and there on the Internet, Visual Studio apparently uses the built project executable to render the UserControl designer(?). In my case, this is probably not it, though, since I use XNA mostly and it doesn't use the UserControl designer.

Solution 5 - Cleaning up resources when application quits

This might be a solution that I have failed to implement properly. I'm just thinking though, that if this is the solution, how come I haven't been required to do it before. I assume XNA unloads everything that gets loaded through the Content pipeline, therefore this solution wouldn't' make any real sense.

If there is anyone that is able to spread some light on this issue, it would be really awesome, as it is stopping me from programming anything really, because I don't like waiting for 10 minutes because I've made a 2 second change all the time.

like image 636
stripe103 Avatar asked Jun 09 '13 11:06

stripe103


People also ask

How do I lock Visual Studio?

To lock a control In the Properties window of Visual Studio, select the Locked property and then select true. (Double-clicking the name toggles the property setting.) Alternatively, right-click the control and choose Lock Controls.

What is the purpose of file locking?

File locking is a mechanism that restricts access to a computer file, or to a region of a file, by allowing only one user or process to modify or delete it at a specific time and to prevent reading of the file while it's being modified or deleted.

How do I lock a file in TFS?

You can also double-click Source Control in Team Explorer. In the Lock dialog box select the file or folder you want. Choose either the Check Out lock or the Check In lock type, and then choose Lock . Learn more about TFVC lock types.

What is lock in Tfs?

Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019 | TFS 2018. You can use the lock command to temporarily prevent changes to a particular file or folder in the source control server.


2 Answers

I've run into this problem a few times myself.

Mine might not be from the same cause as yours, but I'll tell you what went wrong with me and how I fixed it, hopefully it'll be helpful to you.

Basically, my program never fully exited properly, even when it appeared to. It would continue to run, and thus continue to lock down the file.

A quick dirty fix I used initially (and a way to prove if this is the case) is:

  • Open Task Manager (Ctrl-Alt-Del)
  • Click Processes tab
  • Look for your program's name (TileEngine.exe)
  • Note: There will probably be name_vshost.exe (TileEngine_vshost.exe) That's a VisualStudio thing, ignore that, it's not relevant.
  • If you find it, it means your program hasn't actual exited fully.
  • If it's there, click on it and press "End Process"

So if it's there, then for some reason, your program didn't shut down, like mine did.

Often, this is from a thread being launched and forgotten, or an Async task that never completes, or something like that.

Make sure in your OnExiting(..) void function that you kill all running threads.

If your program is still running despite best attempts to close all threads and other blockers, you can use the very dirty bad method: In OnExiting(...) run the code "System.Diagnostics.Process.GetCurrentProcess().Kill();" - this will taskmanager-style forceshutdown the current process... this is only as an emergency I-can't-make-it-work-any-other-way method.

like image 194
mcmonkey4eva Avatar answered Sep 21 '22 14:09

mcmonkey4eva


I think I found the solution myself. In the Project Properties, "Enable the Visual Studio hosting process" wasn't checked. Checking it seems to have fixed the problems, at least for now.

Got reminded of it from mcmonkey4eva's post. So thanks for that =)

And thanks for the other replied I've got. Stackoverflow is awesome!

like image 27
stripe103 Avatar answered Sep 20 '22 14:09

stripe103