none
Continious Code ? RRS feed

  • Question

  • This console app code runs once then close when i hit enter but i need it to be continious wht would i change?

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp3
    {
        class Program
        {
            public Queue VisitedQueue = new Queue();
            public Program myprogram = new Program();
            static void Main()
            {
                //Console.WriteLine("YESSSSSS");
                //Console.ReadKey();
                //Console.WriteLine("M");
                //Console.ReadKey();
    
                
                    int M = Main1();
               
                    Console.WriteLine(M);
                    Console.ReadKey();
                
            }
    
            static int Main1()
            {
                string H;
                int M;
               H = Console.ReadLine();
                M = Convert.ToInt32(H) + 5;
                return M;
                
            }
        }
    }

    Wednesday, February 5, 2020 11:55 AM

All replies

  • You could use this logic and replace true with logic that allows the app to close when needed.

    while (true) {
          System.Console.ReadKey();                
     };


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, February 5, 2020 12:57 PM
    Moderator
  • You could use this logic and replace true with logic that allows the app to close when needed.

    while (true) {
          System.Console.ReadKey();                
     };


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.


    What Karen is saying is that you need a message loop to keep the app persistent.  This is done for you with Windows Forms apps but not with Windows Console apps in DotNET.  The reason is that your console app is supposed to close when finished.

        class Program
        {
            public Queue VisitedQueue = new Queue();
            public Program myprogram = new Program();
            static void Main()
            {
                //Console.WriteLine("YESSSSSS");
                //Console.ReadKey();
                //Console.WriteLine("M");
                //Console.ReadKey();
    
                // This var needs to be scoped to the function and changed in the do
                int M = 0;
                // The following message pump/loop keeps the console app going and going and going
                do
                {
                    M = Main1();
                    if (M > int.MinValue)
                    {
                        Console.WriteLine(M);
                    }
                    else
                    {
                        Console.WriteLine(@"Exited by user command");
                        break;
                    }
                } while (true);
            }
    
            static int Main1()
            {
                string H, H_ci;
                int M;
                H = Console.ReadLine();
                H_ci = H.ToLower();
    
                // Let CLI exit or quit commands terminate the loop.
                if ((H_ci == @"exit") || (H_ci == @"quit"))
                {
                    M = int.MinValue;
                }
                else
                {
                    M = Convert.ToInt32(H) + 5;
                }
                
                return M;
            }
        }


    Wednesday, February 5, 2020 9:13 PM