Solved

C# ConsolApp opens new window

Posted on 2016-09-04
16
71 Views
Last Modified: 2016-10-01
I'm working on a ConsoleApp.  Right now it's simple.  I will be adding functionality to it.  Write now I'm just passing some values to it and writing the results to the console.  I mostly work on winforms and web.

When I run the consoleapp it opens a new window.  Is there a way to run it and display the results on the same window without opening a new window?
0
Comment
Question by:CipherIS
  • 7
  • 6
  • 2
  • +1
16 Comments
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 41784244
consider the Console App as a process flow, once it's executed completely, it will be closed. similarly, once you run it, it will open a new window as it's considered as a new process.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 41784728
Is that how it is supposed to run?  Any way to get it to display in the same command window when I execute it?
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41784826
Can you post the code you are using or if it is a small test project can you zip it up and post to a site where we can download it to see what is the issue? Thanks.
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 1

Author Comment

by:CipherIS
ID: 41784876
I haven't done much yet.  Just testing the consoleapp.  This is what I have right now.  I will expand on it but I want to make sure I'm getting the desired results first.

        public static void Main(string[] args)
        {
            for (int i = 0; i < args.Length; i++)
            {
                Console.WriteLine("{0}", args[i]);
            }

            Console.ReadKey(true);
        }

Open in new window

0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41784887
That should open the applications interface which is the console window and all of its output should be going to it.

So are you saying that you are running the command window, cmd.exe, and executing the ConsoleApplication from it and want to the results from the ConsoleApplication to show up on the command window without the ConsoleApplication showing itself?
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41784951
If you do want to start the program from a command prompt without showing the Console App window start it with the start command like the following if in the same directory.
start /B AppName.exe ParamsHere

Open in new window

Or from not in the same directory
start /B /D "Directory Path Here" AppName.exe ParamsHere

Open in new window

0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 41785414
May be all you need to do is just create an infinite loop in your application?

namespace Looping_in_console
{
    class Program
    {
        static void Main(string[] args)
        {
            bool doContinue = true;
            while (doContinue)
            {
                // read input values
                string input = Console.ReadLine();
                // Do you processing and print results
                // On the next iteration you may continue
                Console.WriteLine("You typed '"+ input + "'");

                // To exit - type "exit", for example
                if (input.ToLower() == "exit")
                {
                    doContinue = false;
                    Console.WriteLine("Good bye!");
                    Console.ReadLine();
                }
            }
        }
    }
}

Open in new window

0
 
LVL 1

Author Comment

by:CipherIS
ID: 41790259
An infinite loop?
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41790268
Hi CipherIS; did you try my last post?
0
 
LVL 1

Author Comment

by:CipherIS
ID: 41790283
Yes, it didn't work.  Still popped open another window.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 41790322
Yes. I'm not quite getting your requirements. If you just need to run your application, enter your data, display results, and repeat it again and again without restarting your console application - you may use the approach I showed in my example. If you need something else - forget.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41790401
Can you post the steps you took to use the solution I posted, because if you used the /D switch it should not have opened a new window.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 41795205
When I type
start /B AppName.exe ParamsHere

Open in new window

or
start /B /D "Directory Path Here" AppName.exe ParamsHere

Open in new window

They both start the app in a new window.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41795265
Here is the Microsoft documentation for the DOS command-line command [start] please note the /b switch. It does work like that on my system. I don't understand why it is not working that way on yours. Can you use lower case b in command line? Also what OS version are you using?
0
 
LVL 1

Author Comment

by:CipherIS
ID: 41811729
Windows 10
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41811742
I also am using Windows 10 myself.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question