Brian Ogden
Brian Ogden

Reputation: 19232

How to stop file locking of .exe with a Sandbox Console Application, Visual Studio 2012

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();
    }
}

Upvotes: 1

Views: 1066

Answers (3)

Usama Aziz
Usama Aziz

Reputation: 279

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.

Upvotes: 0

Brian Ogden
Brian Ogden

Reputation: 19232

@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.

Upvotes: 0

Anton&#237;n Lejsek
Anton&#237;n Lejsek

Reputation: 6103

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

Upvotes: 1

Related Questions