IOException when dynamically compiling code

I have the following line of code:

CSharpCodeProvider c = new CSharpCodeProvider(); CompilerParameters cp = new CompilerParameters(); cp.ReferencedAssemblies.Add("system.dll"); cp.CompilerOptions = "/t:library"; cp.GenerateInMemory = true; CompilerResults cr = c.CompileAssemblyFromSource(cp, sb.ToString()); 

The result is an IOException .

Additional information: the process cannot access the file 'C: \ Users \ Username \ AppData \ Local \ Temp \ dgl5fb1i.err' because it is being used by another process.

However, this is part of a large program that consumes more than 8 GB of RAM. On a system with 16 GB of RAM, this exception is not thrown. Code that dynamically compiles well compiles and runs. The program works without errors in a system with sufficient RAM. The program is compiled for x64. Please note that I am not getting an OutOfMemoryException or any indication that the program has no memory. In Task Manager, memory usage almost reaches the top before an IOException is thrown.

What could be causing this behavior, and can anyone suggest a solution?

EDIT

I modified the application to use significantly less memory. The error persists, although the application has enough available memory. The problem still occurs on only one machine.

This may be due to the following message: Prevent AssemblyFromSource from compiling to generate temp files with a double file name .

+6
source share
1 answer

I performed several compilations in parallel. It was necessary to change the temporary directory for each compilation so that conflicts did not arise.

 CSharpCodeProvider prov = new CSharpCodeProvider(); CompilerParameters parms = new CompilerParameters(); parms.TempFiles = new TempFileCollection(tempdir); 
+4
source

All Articles