C# 程式啟動其他進程程式

來源:互聯網
上載者:User

標籤:style   os   art   for   io   代碼   

1  啟動一個獨立進程,需要用到的命名空間是:using System.Diagnostics;   進程類是 Process ,進程的相關參數資訊類是 ProcessStartInfo

2  等待啟動的控制台app代碼:

using System;
using System.Threading;
namespace ShowConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("app start!");
            foreach (string item in args)
            {
                Console.WriteLine(" accept a arg that is {0}", item);

                Thread.Sleep(3000);             
            }

            Console.WriteLine("app stop!");
        }
    }
}

3  啟動模式:  並行和串列模式,注意比較代碼區別。

using System;
using System.Threading;
using System.Diagnostics;

namespace HDTest
{
    class Program
    {
        static void Main(string[] args)
        {           

           for (int i = 0; i < 2; i++)
           {
               //並行: 多個同命執行個體進程一起執行
               RunMutilInstanceProcess(i);

               //串列,一個進程啟動結束後,運行下一個
             //  WaitSonProcess(i);

               Thread.Sleep(2000);

           }

            Console.ReadLine();
        }

        static void RunMutilInstanceProcess(int i)
        {
            string appPath = @"E:\VS2010Code\AppTest\ShowConsoleApp\bin\Debug\ShowConsoleApp.exe";
            ProcessStartInfo process = new ProcessStartInfo();
            process.FileName = appPath;
            process.Arguments = "process " + i.ToString();

            process.UseShellExecute = false;
            process.CreateNoWindow = true;

            process.RedirectStandardOutput = true;
            Process.Start(process);

           // string Result = p.StandardOutput.ReadToEnd();
           // Console.WriteLine("the console app output is {0}", Result);
             Console.WriteLine(" process {0} start", i);
        }

        static void WaitSonProcess(int i)
        {
            Process process = new Process();
            string appPath = @"E:\VS2010Code\AppTest\ShowConsoleApp\bin\Debug\ShowConsoleApp.exe";
            process.StartInfo.FileName = appPath;
            process.StartInfo.Arguments = "process " + i.ToString();

            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;

            process.StartInfo.RedirectStandardOutput = true;

            // process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            // Start the process
            process.Start();

            Console.WriteLine(" process {0} start", i);
            // Wait that the process exits
             process.WaitForExit();

            Console.WriteLine("the process  had exits");

            // Now read the output of the DOS application
            string Result = process.StandardOutput.ReadToEnd();

            Console.WriteLine("the console app output is {0}", Result);
        }
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.