1

I literally think this is the only technical challenge I have been presented with that I have not figured out a way to solve to date.

Using Visual Studio 2012 proffessional, happens in Visual Studio 2010 as well, I use a simple ConsoleApplication to test code, complete coding tests, a scratch pad or Sandbox if you will. For the life of me I cannot stop the errors:

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


Error 11 Could not copy "obj\x86\Debug\ConsoleApplication.exe" to "bin\Debug\ConsoleApplication1.exe". Exceeded retry count of 10. Failed. ConsoleApplication1

I have Process Explorer, every time I try and close the handle I get the following:

enter image description here

So every time I make a simple code change, after Main exits correctly my Console.Application.exe is locked and I have to just sit there and wait a minute or two to actually rebuild a code change and test.

Is there any way to stop this from happening, the Main thread handles disposal on its own, so attempting manual disposal, like Application.Exit is futile, see here.

class Program
{


    static void Main(string[] args)
    {
        var numbers = GetUniqueRandoms(new Random(), 10, 100);

        Console.WriteLine("Numbers before selection sort:");
        foreach (var number in numbers)
        {
            Console.Write("{0},", number);
        }
        Console.WriteLine();
        //selection sort
        var pos_min = 0;
        for (var i = 0; i < numbers.Length - 1; i++)
        {
            pos_min = i;
            for (var j = i + 1; j < numbers.Length; j++)
            {
                if (numbers[j] < numbers[i])
                    pos_min = j;
            }

            if (pos_min != i)
            {
                var temp = numbers[i];
                numbers[i] = numbers[pos_min];
                numbers[pos_min] = temp;
            }
        }

        Console.WriteLine("Numbers after selection sort:");
        foreach (var number in numbers)
        {
            Console.Write("{0},", number);
        }

    }

    static int[] GetUniqueRandoms(Random random, int count, int max)
    {
        var result = new List<int>(count);
        var set = new HashSet<int>();
        for (var i = 0; i < count; i++)
        {
            int num;

            do
            {
                num = random.Next(1, max);
            } while (!set.Add(num));

            result.Add(num);
        }
        return result.ToArray();
    }
}
Trevor Reid
  • 3,310
  • 4
  • 27
  • 46
Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
  • I had issue with this a long time and ended making a pre-build script, that moves the exe into different location. Not a real solution, but worked. – Antonín Lejsek Oct 25 '15 at 00:20
  • this application never exits; the last statement is a perpetual `Console.Readline()`. Are you saying that if you use CTRL+Z to send null and close the `Readline()`, the program still stays in memory? – Claies Oct 25 '15 at 00:21
  • The Console.Readline keeps the Console application window open so I can read the output, I hit enter/return, the application closes – Brian Ogden Oct 25 '15 at 00:38
  • I have removed the Console.ReadLine() and will report if that fixes anything when my .exe unlocks again in a minute or two. – Brian Ogden Oct 25 '15 at 00:40
  • Same file lock occurs with the Console.Readline() removed – Brian Ogden Oct 25 '15 at 00:41
  • @AntonínLejsek, don't you have to create a new distinct folder each time in your pre-build script? – Brian Ogden Oct 25 '15 at 00:42

3 Answers3

1

Not a real solution

Ok, here is the pre-build script. The parsing of %Time% and %Date% depends on localization and maybe would not work for You. Key is the move command, assembly file is moved away and compiler is happy.

For /f "Tokens=2,3,4 Delims=/. " %%i In ("%Date%") Do @(
  Set Month=%%j& Set Day=%%i& Set Year=%%k
)

set ActDate=%Year%_%Month%_%Day%

For /f "Tokens=1,2,3 Delims=/.:, " %%i In ("%Time%") Do @(
  Set Hour=0%%i& Set Min=%%j& Set Sec=%%k
)

set ActTime=%Hour:~-2,2%-%Min%-%Sec%

move c:\\Users\\Antonˇn\\Desktop\\evidence\\EvidenceSolution\Evidence\bin\Debug\evidence.exe c:\\Users\\Antonˇn\\Desktop\\evidence\\garbage\%ActDate%__%ActTime%__evidenceDebug.exe
move c:\\Users\\Antonˇn\\Desktop\\evidence\\EvidenceSolution\Evidence\bin\Release\evidence.exe c:\\Users\\Antonˇn\\Desktop\\evidence\\garbage\%ActDate%__%ActTime%__evidenceRelease.exe

echo 0
Antonín Lejsek
  • 6,003
  • 2
  • 16
  • 18
  • A good idea, I got an error with the script thought, "exited with code 1". My file paths are good but you don't escape all back slashes in your file paths, how come? – Brian Ogden Oct 26 '15 at 20:23
  • It is copying Exes to my garabage folder, one build copies 3 exes, not sure why – Brian Ogden Oct 26 '15 at 20:26
  • Ok, I just added an EXIT 0 in place of echo 0 that is copying exes to my gabarage folder, and I can build and run my ConsoleApplication but I still get the unable to copy exe errors same as before – Brian Ogden Oct 26 '15 at 20:29
0

@AntoninLejesk answer was a good try but didn't work for me. I found a great answer that worked here: Visual Studio 2010 build file lock issues

You create a ConsoleApplication.exe called "VisualStudioLockWorkaround" and call the exe in your pre build script, passing the target path, worked like a charm, big up to @Godeke for the solution.

Community
  • 1
  • 1
Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
0

Both of above discussed solutions didn't worked for me. I don't know how but giving a restart to my pc saved my life.

Usama Aziz
  • 169
  • 1
  • 10