【发布时间】:2014-10-30 11:02:55
【问题描述】:
我有一个小型 Windows 应用程序,用户在其中输入代码并在运行时编译按钮单击事件代码。
当我第一次单击按钮时,它工作正常,但如果多次单击同一个按钮,则会出现错误“该进程无法访问 Exmaple.pdb 文件,因为它正在被另一个进程使用。” .下面是示例示例代码
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "Example" + ".exe", true); //iloop.ToString() +
parameters.GenerateExecutable = true;
CompilerResults results = csc.CompileAssemblyFromSource(parameters,
@"using System.Linq;
class Program {
public static void Main(string[] args) {}
public static string Main1(int abc) {" + textBox1.Text.ToString()
+ @"
}
}");
results.Errors.Cast<CompilerError>().ToList().ForEach(error => Error = error.ErrorText.ToString());
var scriptClass = results.CompiledAssembly.GetType("Program");
var scriptMethod1 = scriptClass.GetMethod("Main1", BindingFlags.Static | BindingFlags.Public);
StringBuilder st = new StringBuilder(scriptMethod1.Invoke(null, new object[] { 10 }).ToString());
result = Convert.ToBoolean(st.ToString());
}
}
}
我该如何解决这个问题,这样如果我多次点击同一个按钮......它应该可以正常工作。
谢谢,
【问题讨论】:
-
有什么线索可以解决这个问题吗??
标签: c# compilation runtime ioexception