我發現自己處於令人討厭的情況,即在調試會話完成後,visual studio調試器不能自行釋放。因此,devenv.exe的保留對可執行文件的鎖,我不能重建項目,由於可怕的錯誤:以後每運行週期是不強制Visual Studio 2008調試器發佈可執行文件
Error 1 Unable to copy file "obj\Debug\program.exe" to "bin\Debug\program.exe". The process cannot access the file 'bin\Debug\program.exe' because it is being used by another process.
這可以通過重新啓動Visual Studio中,但重新啓動我的整個IDE固定恰好有利於良好的編碼環境。建立 - >清潔沒有幫助。
我google了這個錯誤,雖然症狀似乎是相當普遍的基本原因是不同的。主要我想知道,按重要性排序:
- 是否有任何快速的方法來解鎖文件,而無需重新啓動Visual Studio?
- 除此之外,我應該採用什麼防禦性編程技術來防止這種情況?
- 在下面的例子中幕後究竟發生了什麼,導致調試器不能釋放?
一個會產生這種症狀的代碼的例子就在這裏。
class Program
{
static void Main(string[] args)
{
var f1 = new Form1();
f1.Dir = "asdf";
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private FileSystemWatcher fsw;
public string Dir
{
get { return fsw.Path;}
set
{
fsw = new FileSystemWatcher(value);
fsw.EnableRaisingEvents = true;
throw new Exception("asdf");
}
}
~Form1()
{
if (fsw != null)
fsw.Dispose();
}
}
重現:
- 運行Program.Main使用Visual Studio 2008中的調試器。
- 當拋出異常時停止調試。
- 更改源代碼並嘗試重建。
編輯:各種各樣的解決方案:
public Form1()
{
InitializeComponent();
this.Closing += (sender, args) =>
{
if (watcher != null)
watcher.Dispose();
};
}
我仍然有興趣在爲什麼這個工作,並同時將其放置在析構函數沒有。
您使用的是Windows 7嗎? – 2009-12-24 16:11:43
沒有。帶有VS 9.0.30729.1 SP的Windows XP SP3。這是爲.NET 3.5 SP1構建的項目 – fostandy 2009-12-24 16:23:53