IOException动态编译代码时 [英] IOException when dynamically compiling code

查看:73
本文介绍了IOException动态编译代码时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码行:

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

这将导致抛出IOException.

其他信息:进程无法访问文件 'C:\ Users \ Username \ AppData \ Local \ Temp \ dgl5fb1i.err',因为它是 被另一个进程使用.

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

但是,这是消耗超过8GB RAM的大型程序的一部分.在具有16GB RAM的系统上,不会引发此异常.动态编译的代码可以很好地编译并运行.该程序在具有足够RAM的系统上运行而不会出现任何错误.该程序针对x64进行编译.请注意,我没有得到OutOfMemoryException或任何表明程序内存不足的指示.在任务管理器中,在抛出IOException之前,内存使用率几乎达到顶部.

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

是什么原因导致这种行为,任何人都可以提出解决方案?

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

编辑

我修改了应用程序以使用更少的内存.即使应用程序具有足够的可用内存,该错误仍然存​​在.该问题仍然仅在一台计算机上发生.

I modified the application to use substantially less memory. The error persists even though the application has enough available memory. The issue still occurs only on one machine.

这可能与以下帖子有关:防止CompileAssemblyFromSource生成具有重复文件名的临时文件.

This could be related to the following post: Prevent CompileAssemblyFromSource from generate temp files with duplicate file name.

推荐答案

我正在并行运行多个编译.有必要为每个编译更改临时目录,以免发生冲突.

I was running several compilation in parallel. It was necessary to change the temporary directory for each compilation, so that conflicts do not occur.

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

这篇关于IOException动态编译代码时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆