Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Threading;
- namespace AspnetRegsqlHelper {
- public class AspNetProcess {
- public string AspNetExePath { get; set; }
- volatile bool _running = false;
- volatile bool _userCancelled = false;
- Action<string> _processOutput;
- Action _processFinished;
- AsyncOperation _outputOperation;
- AsyncOperation _finishedOperation;
- bool _synchronized;
- public bool HasErrors { get; set; }
- public AspNetProcess()
- : this(false) {
- }
- public AspNetProcess(bool synchronized) {
- _synchronized = synchronized;
- }
- #region AspNet Reg Process
- public void CancelProcess() {
- _userCancelled = true;
- }
- public void StartProcess(string args,Action<string> processOutput,Action processFinished) {
- if(_running)
- throw new InvalidOperationException("Already started");
- _processOutput = processOutput;
- _processFinished = processFinished;
- _running = true;
- _userCancelled = false;
- HasErrors = false;
- _outputOperation = AsyncOperationManager.CreateOperation(null);
- _finishedOperation = AsyncOperationManager.CreateOperation(null);
- Action<string> writeOutput = (s) => {
- _outputOperation.Post((state) => {
- if(_processOutput != null)
- _processOutput(s);
- },s);
- };
- Action finshish = () => {
- if(_processFinished != null) {
- _finishedOperation.Post((nothing) => {
- _processFinished();
- },null);
- }
- };
- writeOutput(args);
- ProcessStartInfo info = new ProcessStartInfo(this.AspNetExePath);
- info.CreateNoWindow = true;
- info.RedirectStandardOutput = true;
- info.RedirectStandardInput = true;
- info.RedirectStandardError = true;
- info.UseShellExecute = false;
- info.Arguments = args;
- Thread processThread = new Thread(delegate(object proccessInfo) {
- Debug.WriteLine(Thread.CurrentThread.Name + " starting");
- ProcessStartInfo procInfo = proccessInfo as ProcessStartInfo;
- Process aspNetProcess = Process.Start(procInfo);
- aspNetProcess.Exited += (s,e) => {
- _running = false;
- };
- aspNetProcess.OutputDataReceived += (s,e) => {
- string output = e.Data == null ? "" : e.Data.ToString();
- if(output.Contains("Finished."))
- _running = false;
- else if(output.Contains("exception") || output.Contains("error")) {
- HasErrors = true;
- _running = false;
- }
- writeOutput(e.Data);
- };
- aspNetProcess.ErrorDataReceived += (s,e) => {
- HasErrors = true;
- writeOutput(e.Data);
- };
- aspNetProcess.BeginOutputReadLine();
- //wait until finished or cancelled
- while(_running) {
- if(_userCancelled)
- break;
- Thread.Sleep(5);
- }
- aspNetProcess.CancelOutputRead();
- aspNetProcess.Close();
- try {
- aspNetProcess.Kill();
- }
- catch { }
- finally {
- aspNetProcess.Dispose();
- _running = false;
- }
- finshish();
- });
- processThread.Name = "Run aspnet_reg.exe Process";
- processThread.IsBackground = true;
- processThread.Start(info);
- if(_synchronized)
- processThread.Join();
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment