在VS2012 c#项目的构建过程中,我一直得到这个错误

Error   41  Could not copy "obj\Debug\WeinGartner.WeinCad.exe" to
 "bin\Debug\WeinGartner.WeinCad.exe". 
 Exceeded retry count of 10. Failed.    


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

现在我知道该终止进程了

Weingartner.WeinCad.vhost.exe

(有时)有用,但这让我很紧张。有办法阻止这一切发生吗?

调试器设置为


当前回答

在我的情况下,它是Resharper单元测试运行器(加上NUnit测试,从来没有这样的问题与MsTests)。在杀死进程后,可以重建进程,而无需重新启动OS或VS2013。

其他测试运行程序,比如xUnit,也会导致同样的问题。

有帮助的方法是检查是否可以添加Dispose模式,例如,如果您正在添加DbFixture,而数据库联系人没有正确地处理。这将导致即使测试完成,程序集文件也被锁定。

请注意,您可以将IDisposable接口添加到DbFixture中,并让IntelliSense添加Dispose模式。然后,处理相关的包含属性,并显式地将它们赋值为null。

这将有助于以一种干净的方式结束测试,并在测试结束后立即解锁相关的锁定文件。

示例(DBFixture由xUnit测试使用):

public class DbFixture: IDisposable
{
    private bool disposedValue;
    public ServiceProvider ServiceProvider { get; private set; }
    
    public DbFixture()
    {
        // initializes ServiceProvider
    }
    
    
    protected virtual void Dispose(bool disposing)
    {
        if (!disposedValue)
        {
            if (disposing)
            {
                // dispose managed state (managed objects)
                ServiceProvider.Dispose();
                ServiceProvider = null;
            }

            // TODO: free unmanaged resources (unmanaged objects) and override finalizer
            // TODO: set large fields to null
            disposedValue = true;
        }
    }

    // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
    // ~DbFixture()
    // {
    //     // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
    //     Dispose(disposing: false);
    // }

    public void Dispose()
    {
        // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
        Dispose(disposing: true);
        GC.SuppressFinalize(this);
    }
}

测试类本身也需要相同的模式——它需要自己的Dispose方法(如上面的DbFixture类所示):

   public SQL_Tests(ITestOutputHelper output)
    {
        this.Output = output;
        var fixture = new DbFixture(); // NOTE: MS Dependency injection framework didn't initialize when the fixture was a constructor param, hence it is here
        _serviceProvider = fixture.ServiceProvider;
    } // method

因此,它需要在自己的dispose方法中处置其本地属性_serviceProvider,因为测试类构造函数SQL_Tests实例化了它。

其他回答

我在Visual Studio 2013中遇到过类似的错误消息。

大多数情况下,我发现这种情况发生在调试进程因为异常而停止时。

当clean+build不能为我解决这个问题时,我通过以下方法取得了成功:

关闭Visual Studio 删除bin和obj文件夹 重新开放Visual Studio。

此“bug”自Visual Studio 2003以来一直存在。

最后,我还发现我通常可以通过简单地重命名可执行文件然后删除它来克服这个问题。

看看另一个答案。基本上,你可以让MSBuild.exe进程在后台运行,消耗资源文件。如果你有任何构建前或构建后的任务,导致MSBuild通过命令行启动,尝试添加"/nr:false"标志到这个命令。但是,关于更具体的细节,请参见前面的回答。

在我的情况下,它是Resharper单元测试运行器(加上NUnit测试,从来没有这样的问题与MsTests)。在杀死进程后,可以重建进程,而无需重新启动OS或VS2013。

其他测试运行程序,比如xUnit,也会导致同样的问题。

有帮助的方法是检查是否可以添加Dispose模式,例如,如果您正在添加DbFixture,而数据库联系人没有正确地处理。这将导致即使测试完成,程序集文件也被锁定。

请注意,您可以将IDisposable接口添加到DbFixture中,并让IntelliSense添加Dispose模式。然后,处理相关的包含属性,并显式地将它们赋值为null。

这将有助于以一种干净的方式结束测试,并在测试结束后立即解锁相关的锁定文件。

示例(DBFixture由xUnit测试使用):

public class DbFixture: IDisposable
{
    private bool disposedValue;
    public ServiceProvider ServiceProvider { get; private set; }
    
    public DbFixture()
    {
        // initializes ServiceProvider
    }
    
    
    protected virtual void Dispose(bool disposing)
    {
        if (!disposedValue)
        {
            if (disposing)
            {
                // dispose managed state (managed objects)
                ServiceProvider.Dispose();
                ServiceProvider = null;
            }

            // TODO: free unmanaged resources (unmanaged objects) and override finalizer
            // TODO: set large fields to null
            disposedValue = true;
        }
    }

    // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
    // ~DbFixture()
    // {
    //     // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
    //     Dispose(disposing: false);
    // }

    public void Dispose()
    {
        // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
        Dispose(disposing: true);
        GC.SuppressFinalize(this);
    }
}

测试类本身也需要相同的模式——它需要自己的Dispose方法(如上面的DbFixture类所示):

   public SQL_Tests(ITestOutputHelper output)
    {
        this.Output = output;
        var fixture = new DbFixture(); // NOTE: MS Dependency injection framework didn't initialize when the fixture was a constructor param, hence it is here
        _serviceProvider = fixture.ServiceProvider;
    } // method

因此,它需要在自己的dispose方法中处置其本地属性_serviceProvider,因为测试类构造函数SQL_Tests实例化了它。

这发生在我使用IL支持插件时。

当您的项目中没有任何IL文件时(例如,因为您删除了最后一个文件),构建将失败,如问题中所述。

删除对IL的支持解决了这个问题

对我来说,杀死vste .executionengine.exe进程(es)在90%的情况下解决了这个问题。如果这不起作用,那么也杀死QTAgent32.exe,然后删除项目的/bin和/obj文件夹。

这是我工作中最烦人的部分。:)