Note that there are some explanatory texts on larger screens.

plurals
  1. POFile being used by another process even if closed it before
    primarykey
    data
    text
    <p>I don't understand why the same code runs successfully on a 32-bit system, but trying to run it on a 64-bit system, always gives an error:</p> <blockquote> <p>File being used by another process</p> </blockquote> <p>I declare a file watcher event to watching a file changed. </p> <p>My code is similar to below:</p> <pre><code>static void Main() { //init load file LoadAssembly(dll); //bind watching event. FileSystemWatcher watcher = new FileSystemWatcher(rootPath, "*.dll"); watcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite; watcher.Changed += new FileSystemEventHandler(TemplateFile_Changed); watcher.IncludeSubdirectories = false; watcher.EnableRaisingEvents = true; } private LoadAssembly(assemblyFile) { try { using (FileStream fs = new FileStream(assemblyFile, FileMode.Open, FileAccess.Read, FileShare.Read)) { //read a file and write to memorystream. ...... fs.Close(); fs.Dispose(); } asm = Assembly.Load(ms.ToArray()); } finally { ms.Close(); } } private void TemplateFile_Changed(object sender, FileSystemEventArgs e) { lock (_sync) { LoadAssembly(e.FullPath); } } </code></pre> <p>I test code that can work in 32-bit system(in my computer). When I deployed to 64-bit system, when I change a file and tell application need to re-load this file ,that always get a</p> <blockquote> <p>File being used by another process`</p> </blockquote> <p>at</p> <pre><code>using (FileStream fs = new FileStream(assemblyFile, FileMode.Open, FileAccess.Read, FileShare.Read)) </code></pre> <p>updated-1</p> <p>Well,thanks @Hans Passant answered,this exception it's looks caused by the file watcher that fired a changed event and still keep file open. I add a Thread.Sleep() in my TemplateFile_Changed looks solved this problem.</p> <pre><code>private void TemplateFile_Changed(object sender, FileSystemEventArgs e) { lock (_sync) { Thread.Sleep(2000); LoadAssembly(e.FullPath); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload