-1

I'm not going to go into details why am trying to do this, instead of making the main application do the work. I think it's currently easier for me. But I'm not going to use this technique in the future.

In my case, the main form has a button that opens another form. In the second one for you can adjust the amount, pause, resume and stop the work of the console application (sound totally useless (and maybe stupid), but, like I said, I'm not going to go into details why). This means that the application must have access to all the variables and resources of the whole program and vise versa.

I know how to launch a new form trough a main form, but I don't know how to launch a console application.

EDIT:

I forgot to mention, that the console application is a part of the solution.

AlexSavAlexandrov
  • 858
  • 2
  • 13
  • 34

4 Answers4

2

Your requirement is a bit vague; "the application must have access to all the variables and resources of the whole program and vise versa". 'Variables and resources' cannot be shared across processes, you will instead need interprocess communication of some form.

If your console app merely needs to communicate back to the calling forms app that a RPC has succeeded then use exit codes in the console app, see: How do I return a value from a console application to a service in .NET?

Otherwise this has been answered before: Getting the ouput from Console window into Winform application

Community
  • 1
  • 1
Steve O
  • 228
  • 1
  • 9
  • In the sentence "the application must have access to all the variables and resources of the whole program and vise versa" with "the application" I meant "the console application" and with "the whole program" I meant all the stuff in the solution that gets compiled. In my case I don't need to return values from the console application. – AlexSavAlexandrov Oct 23 '12 at 21:29
1

You can just call Main directly. Beware of doing this on the UI thread directly though!

SomeConsoleApp.Main(new string[]{"-O", "File 1.txt", "-some-parameter"});

Or if you only have an exe, you can do:

System.Diagnostics.Process.Start("someconsoleapp.exe");
johv
  • 4,424
  • 3
  • 26
  • 42
1

You'll need to either create a console emulator (time consuming and difficult to get right), or fire up cmd.exe in another process and use remote procedure calls (or another inter process communication method) to communicate between the two processes

Pete Baughman
  • 2,996
  • 2
  • 19
  • 33
1

If you want to communicate between the two processes, take a look at this library here:

https://github.com/TheCodeKing/XDMessaging.Net

It allows you to send messages from one app to the other. For example, App1 sends a message "stop" on the channel "randomkey" to ConsoleApp1, ConsoleApp1 can listen on the channel "randomkey" and intercept the "stop" message and stop its current processing.

If you wanted to just open the console window, just use System.Diagnostics.Process.Start();

Lunyx
  • 3,164
  • 6
  • 30
  • 46
  • In my case there isn't much communication: Just a few boolean variables that are being changed in the second form. Changes in the values of the variables cause the console application to stop, pause and resume the process. And there are 2 integers that adjust the amount of work before the work starts. The communication will happen with variables, not commands. – AlexSavAlexandrov Oct 23 '12 at 21:18
  • The messages sent are strings, so you can parse those within your Console Application and change the values based on the contents of the string received. The listener is added as an event handler, so you just need to put your code to parse the string and modify the variables from within the event method. Whenever you detect a change in your main application through an event, you can use that event to send over a string to your console app. – Lunyx Oct 23 '12 at 21:30