当前位置: 代码迷 >> .NET Framework >> 关于Process.Start()进程有关问题~
  详细解决方案

关于Process.Start()进程有关问题~

热度:205   发布时间:2016-05-01 23:25:42.0
关于Process.Start()进程问题~~求助~~
我的是Winform程序,需要点击Click的时候启动一个外部的进程

我在窗口的Load方法中初始化了一个Process成员,默认打开了一个外部进程,代码如下:

                this.FileProcess = new Process();
                this.FileProcess .StartInfo.UseShellExecute = false;
                this.FileProcess .StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                this.FileProcess .StartInfo.CreateNoWindow = true;
                this.FileProcess .StartInfo.RedirectStandardInput = true;
                this.FileProcess .StartInfo.RedirectStandardOutput = true;
                this.FileProcess .StartInfo.RedirectStandardError = true;
                this.FileProcess .ErrorDataReceived += errorData;
                this.FileProcess .OutputDataReceived += outputData;
                this.FileProcess .StartInfo.FileName = this.fileFullName;
                this.FileProcess .StartInfo.WorkingDirectory = this.WorkDire;

                this.FileProcess .Start();

                 this.FileProcess .BeginErrorReadLine();
                 this.FileProcess .BeginOutputReadLine();
                 this.FileProcess .StandardInput.AutoFlush = true;



然后我手动关闭之后,想再点击按钮的时候重新打开,Button的Click事件代码如下:

           if (this.FileProcess .HasExited == false)
            {
                this.FileProcess .Kill();
            }
          
            this.FileProcess .Start();


发现无法启动外部进程, this.FileProcess ,必须重新初始化FileProcess 之后才能打开外部进程,为什么呢??

------解决思路----------------------
这和线程的生命周期是一样的吧,你终止一个线程后还能再strat吗,Process同理
------解决思路----------------------
引用:
Quote: 引用:

这和线程的生命周期是一样的吧,你终止一个线程后还能再strat吗,Process同理


但是当我这样打开记事本的会后,关闭打开关闭打开 确实整场的

你打开的是记事本的exe,还是仅仅打开了一个txt?
txt不是进程,它只是个文本文件,因为做了文件关联,所以会自动启动一个进程来打开它
------解决思路----------------------
this.FileProcess 已经被Kill了 还 咋个 start
------解决思路----------------------
进程杀死后就不能再开始了,需再创建。。。
  相关解决方案