Arian Motamedi
Arian Motamedi

Reputation: 7423

Writing to ConsoleApplication project from a different project in C#

I have a WinForms and a ConsoleApplication project. I start both projects at the startup and want to write "live" information to the console application FROM the WinForms app. The console app is pretty simple:

 static void Main(string[] args)
 {
     while (true)
     {
         // This prevents the console from dying
         Console.ReadKey();
     }
  }

  public static void WriteMessage(string message)
  {
     Console.WriteLine(message);
  } 

And I call the WriteMessage function from the WinForms app, but this doesn't work. I put a breakpoint in the WriteMessage method and it gets hit, but nothing is put on the console screen, which makes me think it's because of the while loop in main. I even tried putting the while loop in a different thread but still no success. Any help would be appreciated.

Upvotes: 2

Views: 321

Answers (1)

SLaks
SLaks

Reputation: 888185

Consoles belong to processes, not Visual Studio projects

Most applications (including your WinForms app) have a hidden console.
Calling the console project's WriteMessage() from the WinForms process still writes to the WinForms process' console, which is why you aren't seeing anything..

Instead, you should delete the console project, then set the Output Type of the WinForms app (in Project Properties) to Console application.
This will make the WinForms app have a visible console, so that Console.WriteLine would just work.

Upvotes: 6

Related Questions