我刚刚创建了一个基本控制台应用程序,它使用我的框架从文本文件编译C#代码。在C中保持控制台应用程序活着#
你运行这个的方式是“SecureCodeBridge.exe File.txt”,这个文本文件包含了创建和显示可以正常工作的窗体的代码。
的问题是,因为我的应用程序是一个控制台应用程序,它会立即退出从文本文件的方法被调用(表格显示了第二和消失的控制台应用程序退出...)
这是我的控制台应用程序的代码:
static void Main(string[] args)
{
string entString = args[0];
if(System.IO.File.Exists(entString))
{
string entContents = System.IO.File.ReadAllText(entString);
SecureCode sC = new SecureCode(entContents);
CompilerResults cR = sC.Compile();
if(cR.Errors.Count > 0)
{
//Abort!
foreach(CompilerError cE in cR.Errors)
{
Console.WriteLine(cE.ErrorText);
}
}
else
{
sC.Run(cR);
}
}
}
这是我的框架使用编译和运行代码的代码:
public class SecureCode
{
string code;
public SecureCode(string source)
{
code = source;
}
public CompilerResults Compile()
{
Dictionary<string, string> providerOptions = new Dictionary<string, string>
{
{"CompilerVersion", "v3.5"}
};
CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);
CompilerParameters compilerParams = new CompilerParameters
{
GenerateInMemory = true,
GenerateExecutable = false
};
compilerParams.ReferencedAssemblies.Add("System.dll");
compilerParams.ReferencedAssemblies.Add("System.Windows.Forms.dll");
compilerParams.ReferencedAssemblies.Add("System.Drawing.dll");
CompilerResults results = provider.CompileAssemblyFromSource(compilerParams, code);
return results;
}
public void Run(CompilerResults results)
{
object o = results.CompiledAssembly.CreateInstance("Code.Class");
MethodInfo mi = o.GetType().GetMethod("Main");
mi.Invoke(o, null);
}
}
是有些方法可以在调用方法时保持控制台应用程序的活动? (例如,被示出的形式)
我试过第二种方法,但它保持控制台应用程序活着(冻结)它也将冻结窗体本身,任何想法? (尝试在不同的线程上运行它,但随后我们又回到了原来的形式,并一直退出) – user265889
在单独的线程上运行调用到循环。弱言谈仍将维持到形式超出范围。 –