private async Task CompileProgram(StudentWork studentWork) { ProcessStartInfo info = new ProcessStartInfo(msbuild); info.RedirectStandardOutput = true; info.RedirectStandardInput = true; info.CreateNoWindow = true; info.UseShellExecute = false; info.WorkingDirectory = Path.GetDirectoryName(studentWork.SourceDirectory); Process proc = new Process(); proc.StartInfo = info; proc.Start(); for (int i = 0; i < 10; i++) { proc.StandardInput.WriteLine(); } var task = proc.StandardOutput.ReadToEndAsync(); var result = await task; if (!proc.HasExited) { proc.Kill(); } studentWork.CompilationOutput = result; string[] lines = result.Split('\n'); string exeline = lines.LastOrDefault(ln => ln.Trim().EndsWith(".exe")); if (exeline != null) { studentWork.ExeFileName = exeline.Substring(exeline.IndexOf("->") + 2).Trim(); } }
private void SearchDirectory(StudentWork studentWork) { DirectoryInfo currentDir = new DirectoryInfo(textBox1.Text); if (Directory.GetDirectories(currentDir.FullName).Any(dir => dir.Contains(studentWork.ZipFileName))) { studentWork.Directory = Directory.GetDirectories(currentDir.FullName) .First(dir => dir.Contains(studentWork.ZipFileName)); } else { studentWork.Directory = ExtractFile(studentWork.ZipFileName); } studentWork.SourceDirectory = FindSourceCode(studentWork.Directory, "*.cs"); var solutionDirectory = FindSourceCode(studentWork.Directory, "*.sln"); if (solutionDirectory != "No Code") { studentWork.SolutionFileName = Directory.GetFiles(solutionDirectory, "*.sln")[0]; } if (studentWork.SourceDirectory != "No Code") { studentWork.CodeFileNames = Directory.GetFiles(studentWork.SourceDirectory, "*.cs") .Select(Path.GetFileName).ToList(); } }
private async Task ProcessStudent(StudentWork studentWork) { SearchDirectory(studentWork); if (studentWork.SourceDirectory != "No Code") { await CompileProgram(studentWork); await ExecuteProgram(studentWork); } }
private async Task ExecuteProgram(StudentWork studentWork) { if (!_consoleProject) { studentWork.ExeOutput = "Not a console project.\nClick \"Run .exe\" to run"; return; } string filename = studentWork.ExeFileName; string result = ""; if (!File.Exists(filename)) { studentWork.ExeOutput = result; return; } ProcessStartInfo info = new ProcessStartInfo(filename); info.RedirectStandardOutput = true; info.RedirectStandardInput = true; info.CreateNoWindow = true; info.UseShellExecute = false; info.WorkingDirectory = Path.GetDirectoryName(filename); Process proc = new Process(); proc.StartInfo = info; proc.Start(); foreach (string t in textBox2.Lines) { proc.StandardInput.WriteLine(t); } var task = proc.StandardOutput.ReadToEndAsync(); if (await Task.WhenAny(task, Task.Delay(1500)) == task) { result = task.Result; } else { result = ".exe Timeout"; } if (!proc.HasExited) { proc.Kill(); } studentWork.ExeOutput = result; }