结果没有改变 [英] The result is not changing

查看:82
本文介绍了结果没有改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我刚刚创建了一个简单的程序,用于检查进程是否正在运行.这意味着当我运行名为console1的程序时.在此程序中,它表明"Console1当前正在运行".但是,当我关闭console1时,它显示"console1".正在运行",而不是"console1未运行".我应用了循环,goto和console.clear()等...,但没有任何效果.下面是我使用的代码.您能告诉我如何解决它吗?

Hi,
I just created a simple program that checks a process is currently running or not.Which means when i run a program named console1.In this program it shows that the "Console1 is currently running".But,when i closed console1 it showing "console1 is running" instead of "console1 is not running".I applied looping,goto and console.clear() etc...But nothing worked.Following is the code i used.Could you tell how to fix it?

static void Main(string[] args)
{

Process[] myProcesss;
myProcesss = Process.GetProcessesByName("console1.vshost");
if (myProcesss.Length > 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Running");
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("not running");
}
Console.ReadLine();


}


谢谢

推荐答案

这取决于您在循环中正在执行的操作-您必须每次都获取正在运行的进程的列表,否则它将永远不会改变.这是我的方法:
It depends on what you are doing in your loop - you have to get the list of running processes each time round, or it will never change. This is how I do it:
static void Main(string[] args)
    {
    while (true)
        {
        Console.WriteLine(IsRunning("Console2"));
        Thread.Sleep(500);
        }
    }
private static bool IsRunning(string name)
    {
    Process[] processes = Process.GetProcesses();

    foreach (Process p in processes)
        {
        if (p.ProcessName == name)
            {
            return true;
            }
        }
    return false;
    }


您的问题不清楚.但是,您不仅要检查提取的进程数,还要检查进程本身.

可能您的实例也已获取,请检查

Your question is not clear. However, you check not only on number of fetched process but also for the process itself.

Probably your instance is also fetched, check that

Process currentProcess = Process.GetCurrentProcess();



您当前的进程不在检索到的数组内.

每个过程都有其自己的唯一ID.打印出来并为所有检索到的进程打印出它们的ID,并检查并发布结果.


欢呼声



your current process is not inside the retrieved array.

Each process has it''s own unique Id. Print them out and print out for all retrieved processes their id and check and post the result.


Cheers


您的程序会告诉您console1.vshost.exe正在运行,而您认为它没有运行.谁错了您或程序?

任务管理器的进程"选项卡将告诉您是否确实有问题要解决.我想你会很惊讶的!

艾伦.
Your program tells you that console1.vshost.exe is running when you think it is not. Who is wrong you or the program?

The Processes tab of the Task Manager will tell you if there really is a problem to fix. I think you are going to be suprised!

Alan.


这篇关于结果没有改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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