• C#学习笔记——启动其他进程


    方法一:

    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:VS2010CodeAppTestShowConsoleAppinDebugShowConsoleApp.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:VS2010CodeAppTestShowConsoleAppinDebugShowConsoleApp.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);
            }
        }
    }

    方法二:

     var processInfo = new ProcessStartInfo(fullName);
        processInfo.UseShellExecute = false;
        processInfo.WorkingDirectory = Path.GetDirectoryName(fullName);
        Process.Start(processInfo);

    方法三:

    internal Process processKeyBoard;
    processKeyBoard = Process.Start("osk.exe"); //osk.exe为系统的键盘程序的进程名称

    查看进程名称可以通过右键该程序,查看所在文件夹,再查看属性

  • 相关阅读:
    Bloom Filter(布隆过滤器)的概念和原理
    [转]模式匹配的KMP算法详解
    字符串搜索算法
    一个10转14进制的代码(写的很巧)
    几个优秀的js框架的license问题,付费,版权,侵权(extj,jquery,YUI,coolite)
    mysql时间戳转成常用可读时间格式方法两种
    单件模式(Singleton Pattern)
    建造者模式(Builder Pattern)
    [转]U盘隐藏文件恢复程序+源码
    [转载]内存分配 知识,全局,局部,静态变量
  • 原文地址:https://www.cnblogs.com/eve612/p/14809736.html
Copyright © 2020-2023  润新知