/// <summary> /// Executes the job. /// </summary> /// <param name="textFormatter">The test formatter.</param> /// <param name="diagnostics">The diagnostic message handler.</param> public async Task Render(ChromiumElementFormatter textFormatter, IDiagnosticHandler diagnostics) { // Determine the output file string outputFilename = OutputFilename; if (string.IsNullOrWhiteSpace(outputFilename)) { outputFilename = Path.GetFileNameWithoutExtension(Filename) + ".svg"; } // Render if (_circuit != null && _circuit.Count > 0) { await textFormatter.UpdateStyle(_cssScript ?? GraphicalCircuit.DefaultStyle); var doc = _circuit.Render(diagnostics, textFormatter); // Finally write the resulting document to svg using (var writer = XmlWriter.Create(outputFilename, new XmlWriterSettings())) { doc.WriteTo(writer); } diagnostics?.Post(new DiagnosticMessage(SeverityLevel.Info, "JOB01", $"Finished converting '{Filename}', output at '{outputFilename}'.")); } else { diagnostics?.Post(new DiagnosticMessage(SeverityLevel.Error, "JOB02", $"No circuit elements in '{Filename}'")); } }
/// <summary> /// Starts a number of jobs to be converted. /// </summary> /// <param name="jobs">The jobs to convert.</param> /// <param name="diagnostics">The diagnostics handler.</param> public static async Task DoJobs(IReadOnlyList <Job> jobs, IDiagnosticHandler diagnostics) { if (jobs == null || jobs.Count == 0) { return; } // Let's try to do as much as possible in parallel here ChromiumElementFormatter formatter = null; var formatterTask = Task.Run(() => formatter = new ChromiumElementFormatter()); try { var tasks = new Task[jobs.Count]; for (int i = 0; i < jobs.Count; i++) { tasks[i] = jobs[i].Compute(); } Task.WaitAll(tasks); // Render all the completed tasks await formatterTask; if (formatter == null) { diagnostics?.Post(new DiagnosticMessage(SeverityLevel.Warning, "SC001", "Could not create formatter")); } for (int i = 0; i < jobs.Count; i++) { jobs[i].DisplayMessages(diagnostics); if (!jobs[i].HasErrors) { await jobs[i].Render(formatter, diagnostics); } } } finally { // Dispose of our created formatter formatter?.Dispose(); } }