0

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?

Rand Random
  • 7,300
  • 10
  • 40
  • 88
User987
  • 3,663
  • 15
  • 54
  • 115

2 Answers2

1

Not easy to communicate between 2 application

One proposition: When your console Updater starts, you create a file in folder C:\Temps\token.txt. Then, if your console EndlessLoop detects a file names token.txt in C:\Temps, you pause EndlessLoop

Antoine V
  • 6,998
  • 2
  • 11
  • 34
  • 1
    Communication between applications is not at all difficult. There are many examples of using named events in C# for inter-application communication. See, for example, https://stackoverflow.com/q/480430/56778. – Jim Mischel Nov 13 '18 at 15:03
  • @JimMischel yes, but I think my proposition is still simple – Antoine V Nov 13 '18 at 15:06
  • Yes, it's simple. It also requires a busy wait loop or a `FileSystemWatcher`, and can fail if somebody deletes the file or creates it when not expected. It'll work for a non-critical system, but is not at all reliable. I certainly wouldn't recommend using the file system for real-time inter-app communication. Especially when using a named event handle is simple and reliable. – Jim Mischel Nov 13 '18 at 15:11
1
public static bool IsAppRunning()
{
    foreach (Process process in Process.GetProcesses())
    {
        if (process.ProcessName.Contains("Updater"))
        {
            return true;
        }
    }

    return false;
}

If you call this in while loop it tells you if Updater is running or not.

János Tigyi
  • 397
  • 1
  • 9
  • 20
  • Two problems. 1) a busy-wait loop is a waste of resources. 2) Using "Contains" to find the process name is just begging for problems. For example, if some other process called "CriticalFileUpdater" starts, your code is going to return `true` when it shouldn't. – Jim Mischel Nov 13 '18 at 15:14
  • busy loop wait, yeah, use sleep or anything and wont be busy. Use Equals("Updater.exe") then... – János Tigyi Nov 13 '18 at 15:15
  • Why hack together something when there are simple and reliable tools that are made specifically for this job? Even a sleep loop is a busy wait; it's just a bit less busy than a loop without a sleep. Use the right tool for the job. – Jim Mischel Nov 13 '18 at 15:18
  • Wich tool? If you say use the right tool, than say wich one? I don't think you need to optimize this small code, that called micro optimization, 'cos it's so simple and light. – János Tigyi Nov 13 '18 at 15:20
  • Not bad... :D Thanks. (But it works only if you can send that signal from the other app. :p ) – János Tigyi Nov 13 '18 at 15:33