首页 > 解决方案 > C# Pause one console application when other console application starts

问题描述

I have one console application for testing purposes like following:

static void Main(string[] args)
{
    do
    {
        for (int i = 0; i < 10000000; i++)
        {
            Console.WriteLine("Doing some endless loop");
            Console.WriteLine(i.ToString());
        }
    } while (true);
}

As you can see the code is very basic, and I've set it up to endless loop in order to test what I would like to achieve.

The other console application is called "Updater" and I would like to to pause the "EndlessLoop" console application once the "Updater" application is started.

Does anyone knows if this is doable in c# .NET?

标签: c#processconsoleconsole-applicationpause

解决方案


public static bool IsAppRunning()
{
    foreach (Process process in Process.GetProcesses())
    {
        if (process.ProcessName.Contains("Updater"))
        {
            return true;
        }
    }

    return false;
}

如果你在 while 循环中调用它,它会告诉你 Updater 是否正在运行。


推荐阅读