private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { WorkerResult result = e.Result as WorkerResult; FileInfo file = new FileInfo(result.data.csharpShaderFiles[0]); string directory = file.DirectoryName; if (result.builder != null) { try { string log = file.FullName + ".log"; File.WriteAllText(log, result.builder.ToString()); Process.Start("explorer", log); } catch (Exception ex) { string message = string.Format("{0}", ex); MessageBox.Show(message); } } if (e.Error != null) { MessageBox.Show(e.Error.ToString(), "发生异常"); if (MessageBox.Show("是否打开保存结果的文件夹?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { Process.Start("explorer", directory); } } else if (e.Cancelled) { if (MessageBox.Show("您取消了操作,是否打开保存结果的文件夹?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { Process.Start("explorer", directory); } } else { if (MessageBox.Show("操作已完成,是否打开保存结果的文件夹?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { Process.Start("explorer", directory); } } pgbProgress.Value = pgbProgress.Minimum; pgbSingleFileProgress.Value = pgbSingleFileProgress.Minimum; WorkingSwitch(false); }
private void bgWorker_DoWork(object sender, DoWorkEventArgs e) { WorkerData data = e.Argument as WorkerData; int fileCount = data.csharpShaderFiles.Length; int fileIndex = 1; const int magicNumber = 100; WorkerResult result = new WorkerResult(null, data); e.Result = result; StringBuilder builder = new StringBuilder(); foreach (var fullname in this.selectedCSharpShaderFiles) { builder.Append(fileIndex); builder.Append("/"); builder.Append(fileCount); builder.Append(": "); builder.AppendLine(fullname); FileInfo fileInfo = new FileInfo(fullname); string filename = fileInfo.Name; try { CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider(); CompilerParameters objCompilerParameters = new CompilerParameters(); objCompilerParameters.ReferencedAssemblies.Add("CSharpShadingLanguage.dll"); objCompilerParameters.GenerateExecutable = false; objCompilerParameters.GenerateInMemory = true; objCompilerParameters.IncludeDebugInformation = true; CompilerResults cr = objCSharpCodePrivoder.CompileAssemblyFromFile( objCompilerParameters, fullname); if (cr.Errors.HasErrors) { builder.AppendLine(string.Format("编译错误:{0}", fullname)); foreach (CompilerError err in cr.Errors) { Console.WriteLine(err.ErrorText); builder.AppendLine(err.ErrorText); } } else { List <SemanticShader> semanticShaderList = new List <SemanticShader>(); Assembly assembly = cr.CompiledAssembly; Type[] types = assembly.GetTypes(); foreach (var type in types) { if (type.IsSubclassOf(typeof(CSShaderCode))) { CSShaderCode shaderCode = Activator.CreateInstance(type) as CSShaderCode; SemanticShader semanticShader = shaderCode.GetSemanticShader(fullname); semanticShaderList.Add(semanticShader); } } //var semanticShaderList = // from type in cr.CompiledAssembly.GetTypes() // where type.IsSubclassOf(typeof(ShaderCode)) // select (Activator.CreateInstance(type) as ShaderCode).Dump(fullname); foreach (var item in semanticShaderList) { item.Dump2File(); } } } catch (Exception ex) { string message = string.Format("{0}", ex); builder.AppendLine(message); result.builder = builder; } if (result.builder != builder) { builder.AppendLine("sucessfully done!"); } builder.AppendLine(); SingleFileProgress thisFileDone = new SingleFileProgress() { filename = filename, progress = magicNumber, message = string.Format("All is done for {0}", fileInfo.Name), }; bgWorker.ReportProgress(fileIndex++ *magicNumber / fileCount, thisFileDone); } }
private void bgWorker_DoWork(object sender, DoWorkEventArgs e) { WorkerData data = e.Argument as WorkerData; int fileCount = data.csharpShaderFiles.Length; int fileIndex = 1; const int magicNumber = 100; WorkerResult result = new WorkerResult(null, data); e.Result = result; StringBuilder builder = new StringBuilder(); foreach (var fullname in this.selectedCSharpShaderFiles) { builder.Append(fileIndex); builder.Append("/"); builder.Append(fileCount); builder.Append(": "); builder.AppendLine(fullname); FileInfo fileInfo = new FileInfo(fullname); string filename = fileInfo.Name; try { CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider(); CompilerParameters objCompilerParameters = new CompilerParameters(); objCompilerParameters.ReferencedAssemblies.Add("CSharpShaderLanguage.dll"); objCompilerParameters.GenerateExecutable = false; objCompilerParameters.GenerateInMemory = true; objCompilerParameters.IncludeDebugInformation = true; CompilerResults cr = objCSharpCodePrivoder.CompileAssemblyFromFile( objCompilerParameters, fullname); if (cr.Errors.HasErrors) { builder.AppendLine(string.Format("编译错误:{0}", fullname)); foreach (CompilerError err in cr.Errors) { Console.WriteLine(err.ErrorText); builder.AppendLine(err.ErrorText); } } else { List<SemanticShader> semanticShaderList = new List<SemanticShader>(); Assembly assembly = cr.CompiledAssembly; Type[] types = assembly.GetTypes(); foreach (var type in types) { if (type.IsSubclassOf(typeof(CSShaderCode))) { CSShaderCode shaderCode = Activator.CreateInstance(type) as CSShaderCode; SemanticShader semanticShader = shaderCode.Dump(fullname); semanticShaderList.Add(semanticShader); } } //var semanticShaderList = // from type in cr.CompiledAssembly.GetTypes() // where type.IsSubclassOf(typeof(ShaderCode)) // select (Activator.CreateInstance(type) as ShaderCode).Dump(fullname); foreach (var item in semanticShaderList) { item.Dump2File(); } } } catch (Exception ex) { string message = string.Format("{0}", ex); builder.AppendLine(message); result.builder = builder; } if (result.builder != builder) { builder.AppendLine("sucessfully done!"); } builder.AppendLine(); SingleFileProgress thisFileDone = new SingleFileProgress() { filename = filename, progress = magicNumber, message = string.Format("All is done for {0}", fileInfo.Name), }; bgWorker.ReportProgress(fileIndex++ * magicNumber / fileCount, thisFileDone); } }