Ejemplo n.º 1
0
        private static void CheckCSharp(string text, List <KeyValuePair <List <string>, List <string> > > tests, CompilerHandler handler)
        {
            //prepare compiler
            var provider   = new CSharpCodeProvider();
            var parameters = new CompilerParameters
            {
                GenerateExecutable      = true,
                IncludeDebugInformation = true,
                ReferencedAssemblies    = { "System.dll" },
                OutputAssembly          = "program.exe"
            };

            //compile
            var assembly = provider.CompileAssemblyFromSource(parameters, text);

            if (assembly.Errors.HasErrors)
            {
                handler.ErrorCollection = assembly.Errors;
                handler.Output          = assembly.Output.ToString();
                return;
            }

            ProcessStartInfo startInfo = new ProcessStartInfo
            {
                CreateNoWindow         = true,
                UseShellExecute        = true,
                FileName               = assembly.PathToAssembly,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
            };

            try
            {
                // Start the process with the info we specified.
                // Call WaitForExit and then the using statement will close.
                using Process proc       = Process.Start(startInfo);
                proc.Exited             += ((_, _) => handler.Finished = true);
                proc.OutputDataReceived += ((_, args) => handler.Output += args.Data);
                proc.ErrorDataReceived  += ((_, args) => handler.Output += args.Data);
                proc.WaitForExit();
            }
            catch
            {
                handler.Finished = true;
            }
        }
Ejemplo n.º 2
0
 public Task ScheduleCompiling(string text, List <KeyValuePair <List <string>, List <string> > > tests, CompilerHandler handler)
 => _factory.StartNew(() => CheckCSharp(text, tests, handler));