user687554
user687554

Reputation: 11131

Pass arguments to running C# app

I have a C# console app. This console app is intended to run in the background as long as my computer is running. At this time, that console app looks like this:

static void Main(string[] args)
{
  while (true)
  {
    if (args.Length > 0)
    {
      Console.WriteLine("Args received!");
      foreach(var arg in args)
      {
        Console.WriteLine("Arg: " + arg);
      }
    }
  }
}

The console will initially load a lot of data. That's why I want to keep it running instead of always starting it. Still, once it's running, I need a way to "call" it again from another console window.

At this time, I'm thinking of starting the console app shown above using

START /B MyConsole.exe

from the command line. Then, I want to pass data do it in another console window like this:

MyConsole.exe --data [some info here]

This doesn't feel right at all. In fact, I'm fairly confident this won't work. Still, I need some way to have a running console app and pass data to that app from another console window via a command as shown above. The reason why I'm set on the approach shown above is ultimately I'm integrating with a third-party app that let's me execute command line commands. But, because my data is so large, I need to first have that app running.

How can I do this in C#?

Thank you!

Upvotes: 2

Views: 1271

Answers (2)

MistyK
MistyK

Reputation: 6222

I would create self-hosted ASP.NET Core Web Api application which will expose some rest endpoints so you could change your arguments based on the data sent (save it somewhere in memory, whatever). In the background there maybe an implementation of IHostedService running and doing some work in a loop based on the data sent.

Upvotes: 0

nvoigt
nvoigt

Reputation: 77304

The keywords here are inter process communication.

The easiest way to do this might be pipes.

Although not the newest and hippest technology anymore, I think your best bet on making pipes work easily in C# is WCF.

You could always host something else in your console application, like ASP.NET.

Your console application that is holding all the data might be better implemented as a windows service.

Maybe if you data is in fact just files, maybe using a memory mapped file might be easier than the inter process communication.

That said, communication between processes is a lot more complicated than command line parameters. None of those suggestions is particularly easy to implement (at least compared to a console application) and a lot of the details depend on the details of what you want to do.

Upvotes: 2

Related Questions