andrew4582

AspNetProcess

Mar 19th, 2012
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.19 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. using System.Threading;
  5.  
  6. namespace AspnetRegsqlHelper {
  7.     public class AspNetProcess {
  8.         public string AspNetExePath { get; set; }
  9.  
  10.         volatile bool _running = false;
  11.         volatile bool _userCancelled = false;
  12.         Action<string> _processOutput;
  13.         Action _processFinished;
  14.         AsyncOperation _outputOperation;
  15.         AsyncOperation _finishedOperation;
  16.         bool _synchronized;
  17.         public bool HasErrors { get; set; }
  18.  
  19.         public AspNetProcess()
  20.             : this(false) {
  21.         }
  22.         public AspNetProcess(bool synchronized) {
  23.             _synchronized = synchronized;
  24.         }
  25.         #region AspNet Reg Process
  26.  
  27.         public void CancelProcess() {
  28.             _userCancelled = true;
  29.         }
  30.         public void StartProcess(string args,Action<string> processOutput,Action processFinished) {
  31.             if(_running)
  32.                 throw new InvalidOperationException("Already started");
  33.  
  34.             _processOutput = processOutput;
  35.             _processFinished = processFinished;
  36.  
  37.             _running = true;
  38.             _userCancelled = false;
  39.             HasErrors = false;
  40.  
  41.             _outputOperation = AsyncOperationManager.CreateOperation(null);
  42.             _finishedOperation = AsyncOperationManager.CreateOperation(null);
  43.  
  44.             Action<string> writeOutput = (s) => {
  45.                 _outputOperation.Post((state) => {
  46.                     if(_processOutput != null)
  47.                         _processOutput(s);
  48.                 },s);
  49.             };
  50.             Action finshish = () => {
  51.                 if(_processFinished != null) {
  52.                     _finishedOperation.Post((nothing) => {
  53.                         _processFinished();
  54.                     },null);
  55.                 }
  56.             };
  57.             writeOutput(args);
  58.  
  59.             ProcessStartInfo info = new ProcessStartInfo(this.AspNetExePath);
  60.             info.CreateNoWindow = true;
  61.             info.RedirectStandardOutput = true;
  62.             info.RedirectStandardInput = true;
  63.             info.RedirectStandardError = true;
  64.             info.UseShellExecute = false;
  65.             info.Arguments = args;
  66.  
  67.             Thread processThread = new Thread(delegate(object proccessInfo) {
  68.                 Debug.WriteLine(Thread.CurrentThread.Name + " starting");
  69.  
  70.                 ProcessStartInfo procInfo = proccessInfo as ProcessStartInfo;
  71.                 Process aspNetProcess = Process.Start(procInfo);
  72.  
  73.                 aspNetProcess.Exited += (s,e) => {
  74.                     _running = false;
  75.                 };
  76.                 aspNetProcess.OutputDataReceived += (s,e) => {
  77.                     string output = e.Data == null ? "" : e.Data.ToString();
  78.  
  79.                     if(output.Contains("Finished."))
  80.                         _running = false;
  81.                     else if(output.Contains("exception") || output.Contains("error")) {
  82.                         HasErrors = true;
  83.                         _running = false;
  84.                     }
  85.                     writeOutput(e.Data);
  86.                 };
  87.                 aspNetProcess.ErrorDataReceived += (s,e) => {
  88.                     HasErrors = true;
  89.                     writeOutput(e.Data);
  90.                 };
  91.  
  92.                 aspNetProcess.BeginOutputReadLine();
  93.  
  94.                 //wait until finished or cancelled
  95.                 while(_running) {
  96.                     if(_userCancelled)
  97.                         break;
  98.                     Thread.Sleep(5);
  99.                 }
  100.  
  101.                 aspNetProcess.CancelOutputRead();
  102.                 aspNetProcess.Close();
  103.  
  104.                 try {
  105.                     aspNetProcess.Kill();
  106.                 }
  107.                 catch { }
  108.                 finally {
  109.                     aspNetProcess.Dispose();
  110.                     _running = false;
  111.                 }
  112.                 finshish();
  113.             });
  114.             processThread.Name = "Run aspnet_reg.exe Process";
  115.             processThread.IsBackground = true;
  116.             processThread.Start(info);
  117.            
  118.             if(_synchronized)
  119.                 processThread.Join();
  120.         }
  121.  
  122.         #endregion
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment