检测和关闭多个进程? [英] Detecting and closing multiple processes?

查看:100
本文介绍了检测和关闭多个进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个程序打开一个进程进行一些计算,然后不用等待它会打开另一个相同的程序进行不同的计算,但我只希望一次打开5个进程,然后如果有的话是等到它关闭之前的过程才开始新的过程..



这里是我到目前为止...为什么这不起作用以及如何可能会做得更好吗?



Hello there I have a program which opens a process to carry out some calculations and then without waiting it will open another process of the same with different calculations but I only wish for 5 processes at a time to be open and then if there are 5 to wait until the process before it has closed to begin a new process..

here is what i have so far.. why does this not work and how might it be done better?

Dim RunProcess As Integer = process.GetProcessesByName("run").Length
       Dim RunConhost As Integer = process.GetProcessesByName("conhost").Length
       For Each p In process.GetProcessesByName(RunConhost, RunProcess)
           If p.ProcessName.Length > 5 Then
               process.WaitForExit()
               process.Close()
           Else
               process.Start()
               Dim output As String = process.StandardOutput.ReadToEnd()
               RichTextBox1.Text = output
               process.WaitForExit()
               process.Close()
           End If
       Next







我也有点奇怪错误部分通过我的代码运行

无法连接到远程计算机。

我不确定为什么会发生此错误,因为我没有尝试连接到任何人或我程序之外的任何东西?



谢谢提前!!




also i get a strange error part way through my code running
Couldn''t connect to remote machine.
I am not sure why this error happens as I am not trying to connect to anyone or anything outside of my program?

thank you in advance!!

推荐答案

整件事情......好......



首先,你要两次调用Process.GetProcessesByName。为什么?你只需要做一次。一旦你有了这个清单,就可以用它做任何你想做的事。



现在,看看如果陈述表达。您正在将进程名称的长度与5进行比较...您没有比较进程数,您将进程名称的长度与5进行比较。您是否看到了错误那个?



然后就有了你不需要的For Each循环。为什么要枚举进程列表?你只需要知道它们中有多少正在运行。



我简单的方法是轮询进程数。如果有5个进程已经运行,请休眠几秒钟,然后返回并再次检查进程列表。一旦少于5个进程,你就可以开始一个新的进程并返回给调用者。



这不是我做的方式在生产代码中,但您还必须支持此代码并进行维护。凭借您的技能水平,代码越简单,您就越好。
The whole thing is .... well ....

First, you''re calling Process.GetProcessesByName twice. Why? You only need to do it once. Once you have the list, you can do whatever you want with it.

Now, look at that If statement expression. You''re comparing the length of the name of a process to 5... You''re not comparing the number of processes, you''re comparing the LENGTH of the NAME of a process to 5. Do you see something wrong with that?

Then there''s a For Each loop you don''t need. Why are you enumerating a list of processes?? You just need to know how many of them are running.

I simple method would be to poll for the number of processes. If there are 5 processes already running, sleep for a few seconds, then go back and check the processes list again. Once there are less than 5 processes, you can start a new one and return to the caller.

It''s not exactly the way I''d do it in production code, but YOU also have to support this code and maintain it. With your skill level, the simpler the code, the better off you are.


这篇关于检测和关闭多个进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆