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

查看:33
本文介绍了动态编译代码时的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:UsersUsernameAppDataLocalTempdgl5fb1i.err' 因为它是正在被另一个进程使用.

Additional information: The process cannot access the file 'C:UsersUsernameAppDataLocalTempdgl5fb1i.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天全站免登陆