程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 一個C#寫的調用外部進程類(2)

一個C#寫的調用外部進程類(2)

編輯:關於C語言

問題講完了,下面給出這個類的完整代碼

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
namespace Laboratory.Process
{
  class ReadErrorThread
  {
    System.Threading.Thread m_Thread;
    System.Diagnostics.Process m_Process;
    String m_Error;
    bool m_HasExisted;
    object m_LockObj = new object();
    public String Error
    {
      get
      {
        return m_Error;
      }
    }
    public bool HasExisted
    {
      get
      {
        lock (m_LockObj)
        {
          return m_HasExisted;
        }
      }
      set
      {
        lock (m_LockObj)
        {
          m_HasExisted = value;
        }
      }
    }
    private void ReadError()
    {
      StringBuilder strError = new StringBuilder();
      while (!m_Process.HasExited)
      {
        strError.Append(m_Process.StandardError.ReadLine());
      }
      strError.Append(m_Process.StandardError.ReadToEnd());
      m_Error = strError.ToString();
      HasExisted = true;
    }
    public ReadErrorThread(System.Diagnostics.Process p)
    {
      HasExisted = false;
      m_Error = "";
      m_Process = p;
      m_Thread = new Thread(new ThreadStart(ReadError));
      m_Thread.Start();
    }
  }
  class RunProcess
  {
    private String m_Error;
    private String m_Output;
    public String Error
    {
      get
      {
        return m_Error;
      }
    }
    public String Output
    {
      get
      {
        return m_Output;
      }
    }
    public bool HasError
    {
      get
      {
        return m_Error != "" && m_Error != null;
      }
    }
    public void Run(String fileName, String para)
    {
      StringBuilder outputStr = new StringBuilder();
      try
      {
        //disable the error report dialog.
        //reference: http://www.devcow.com/blogs/adnrg/archive/2006/07/14/Disable-Error-Reporting-Dialog-for-your-application-with-the-registry.ASPx
        Microsoft.Win32.RegistryKey key;
        key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"software\microsoft\PCHealth\ErrorReporting\", true);
        int doReport = (int)key.GetValue("DoReport");
        if (doReport != 0)
        {
          key.SetValue("DoReport", 0);
        }
        int showUI = (int)key.GetValue("ShowUI");
        if (showUI != 0)
        {
          key.SetValue("ShowUI", 0);
        }
      }
      catch
      {
      }
      m_Error = "";
      m_Output = "";
      try
      {
        System.Diagnostics.Process p = new System.Diagnostics.Process();
        p.StartInfo.FileName = fileName;
        p.StartInfo.Arguments = para;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.CreateNoWindow = true;
        p.Start();
        ReadErrorThread readErrorThread = new ReadErrorThread(p);
        while (!p.HasExited)
        {
          outputStr.Append(p.StandardOutput.ReadLine()+"\r\n");
        }
        outputStr.Append(p.StandardOutput.ReadToEnd());
        while (!readErrorThread.HasExisted)
        {
          Thread.Sleep(1);
        }
        m_Error = readErrorThread.Error;
        m_Output = outputStr.ToString();
      }
      catch (Exception e)
      {
        m_Error = e.Message;
      }
    }
  }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved