/// <summary> /// Stop currently running script. /// </summary> public static void StopScript() { if (myScriptInterface != null) { myWaitThread.Start(); try { myScriptInterface.Stop(); } catch (Exception e) { if (e.InnerException != null) { PreserveStackTrace(e.InnerException); } else { PreserveStackTrace(e); } Log.LogMessage(e); } Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { })); if (myWaitThread.IsAlive) { myWaitThread.Join(); } OnScriptFinished(); } myScriptAssembly = null; myScriptInterface = null; myThread = null; myWaitThread = null; }
public static bool Compile(string exePath, string sourcecode) { myScriptInterface = null; myScriptAssembly = null; myThread = new Thread(new ThreadStart(RunScript)); myWaitThread = new Thread(new ThreadStart(WaitForStop)); Environment.CurrentDirectory = exePath.Substring(0, exePath.LastIndexOf(@"\")); CodeDomProvider cdp = CodeDomProvider.CreateProvider("C#"); CompilerParameters cp = new CompilerParameters(); cp.ReferencedAssemblies.Add(exePath); AddReferences(sourcecode, cp); cp.GenerateExecutable = false; cp.CompilerOptions = "/optimize+"; cp.GenerateInMemory = true; cp.IncludeDebugInformation = false; CompilerResults CR = cdp.CompileAssemblyFromSource(cp, sourcecode); if (CR.Errors.HasErrors) { StringBuilder ErrorText = new StringBuilder(128); //mainForm.SelectText(CR.Errors[0].Line); foreach (CompilerError CE in CR.Errors) { ErrorText.Append("Error " + CE.ErrorNumber + " on line " + CE.Line.ToString() + ": " + CE.ErrorText + "\r\n"); } MessageBox.Show(ErrorText.ToString(), "Compiler error!"); return(false); } Execute(CR.CompiledAssembly); return(true); }
private static void Execute(Assembly scriptAssembly) { if (scriptAssembly == null) { return; } foreach (Type t in scriptAssembly.GetTypes()) { foreach (Type i in t.GetInterfaces()) { if (i == typeof(IScriptInterface)) { myScriptInterface = (IScriptInterface)scriptAssembly.CreateInstance(t.FullName); myThread.Start(); return; } } } myScriptAssembly = scriptAssembly; myThread.Start(); return; }
public static bool Compile(string exePath, string sourcecode) { myScriptInterface = null; myScriptAssembly = null; myThread = new Thread(new ThreadStart(RunScript)); myWaitThread = new Thread(new ThreadStart(WaitForStop)); Environment.CurrentDirectory = exePath.Substring(0, exePath.LastIndexOf(@"\")); CodeDomProvider cdp = CodeDomProvider.CreateProvider("C#"); CompilerParameters cp = new CompilerParameters(); cp.ReferencedAssemblies.Add(exePath); AddReferences(sourcecode, cp); cp.GenerateExecutable = false; #if DEBUG cp.CompilerOptions = "/debug+"; cp.GenerateInMemory = false; cp.TempFiles = new TempFileCollection(Environment.GetEnvironmentVariable("TEMP"), true); cp.IncludeDebugInformation = true; cp.TempFiles.KeepFiles = true; #else cp.CompilerOptions = "/optimize+"; cp.GenerateInMemory = true; cp.IncludeDebugInformation = false; #endif CompilerResults CR = cdp.CompileAssemblyFromSource(cp, sourcecode); if (CR.Errors.HasErrors) { StringBuilder ErrorText = new StringBuilder(128); //mainForm.SelectText(CR.Errors[0].Line); foreach (CompilerError CE in CR.Errors) ErrorText.Append("Error " + CE.ErrorNumber + " on line " + CE.Line.ToString() + ": " + CE.ErrorText + "\r\n"); MessageBox.Show(ErrorText.ToString(), Strings.Compilererror); return false; } Execute(CR.CompiledAssembly); return true; }
private static void Execute(Assembly scriptAssembly) { if (scriptAssembly == null) return; foreach (Type t in scriptAssembly.GetTypes()) { foreach (Type i in t.GetInterfaces()) { if (i == typeof(IScriptInterface)) { myScriptInterface = (IScriptInterface)scriptAssembly.CreateInstance(t.FullName); myThread.Start(); return; } } } myScriptAssembly = scriptAssembly; myThread.Start(); return; }
/// <summary> /// Stop currently running script. /// </summary> public static void StopScript() { if (myScriptInterface != null) { myWaitThread.Start(); try { myScriptInterface.Stop(); } catch (Exception e) { if (e.InnerException != null) PreserveStackTrace(e.InnerException); else PreserveStackTrace(e); Log.LogMessage(e); } Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { })); if (myWaitThread.IsAlive) myWaitThread.Join(); OnScriptFinished(); } myScriptAssembly = null; myScriptInterface = null; myThread = null; myWaitThread = null; }