Resolved How can I exit my program from a function in another class?

WB1975

Well-known member
Joined
Apr 3, 2020
Messages
87
Programming Experience
Beginner
How can I exit my loop in the main program when the user enters q?

whats the best way to set this up?

i have this class

C#:
using System;

namespace HourlyMeetingScheduler
{
    class Program
    {
        static void Main(string[] args)
        {
            bool programRunning = true;

            while (programRunning) {
                Utility myUtility = new Utility();
                myUtility.WelcomeUser();
                myUtility.PrintMainMenu();
                myUtility.MainMenuLogic();
            }
        }
    }   
}

and this one

C#:
using System;
using System.Collections.Generic;
using System.Text;

namespace HourlyMeetingScheduler
{
    class Utility
    {
        public void WelcomeUser()
        {
            Console.WriteLine("\nWelcome to the hourly scheduler");
            Console.WriteLine("*******************************");
        }

        public void PrintMainMenu()
        {
            Console.WriteLine("\nMain Menu");
            Console.WriteLine("*********");
            Console.WriteLine("\n(a) Add appointment/s");
            Console.WriteLine("(r) Remove appointment/s");
            Console.WriteLine("(e) Edit appointment/s");
            Console.WriteLine("(v) View appointment/s");
            Console.WriteLine("(q) quit");
        }

        public void MainMenuLogic()
        {
            string userChoice = Console.ReadLine();
            char firstLetter = userChoice[0];
            switch (firstLetter)
            {
                case 'a':
                    AddAppointment();
                    break;

                case 'r':
                    RemoveAppointment();
                    break;

                case 'e':
                    EditAppointment();
                    break;

                case 'v':
                    ViewAppointments();
                    break;

                case 'q':
                    //quit loop
                    break;
            }
        }
    }
}
 
What if you could do this in while loop:
C#:
programRunning = myUtility.MainMenuLogic();
 
I just dont understand can you elaborate a bit john sorry
MainMenuLogic is a void method thats not returning anything
how can you make a bool equal a method?
 
bool programRunning = true;
Declare at class level. If you are in another thread for example and a loop is running based on the above boolean being true, you can set it as false from another thread. This will stop the current loop from running.
//quit loop
Set your boolean to false.

Ideally, I would prefer to run your loop on a new thread, and not on your main method. Note that your boolean can't be changed because of the scope of which its defined.
 
Yes, return a logical value that says if it should continue or not.
 
Further more, using @JohnH 's approach will let you rewrite you Main() to something which expresses your intent better:
C#:
Utility myUtility = new Utility();

do
{
    myUtility.WelcomeUser();
    myUtility.PrintMainMenu();
} while(myUtility.MainMenuLogic());
 
Hello, I'm very new to coding but my program is very similar.

i noticed you are using the break in your switch segment.
i do it like this: (my code as example)
Main:
private static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                bool Alive = true;
                while (Alive)
                {
                    Alive = MainCode();
                }
            }
            else
            {
                bool Alive = true;
                while (Alive)
                {
                    Alive = PrefixMainCode(args);
                }
            }
        }


and this is my example switch:


C#:
private static bool MainCode()
        {
            _ = new MenuDrawer("Main Menu", "Input: ", false);
            switch (Console.ReadLine())
            {
                case "rdp":
                    Openrdp();
                    return true;

                case "mkdir":
                    _ = new FolderCreater();
                    return false;

                case "mail":
                    _ = new MailSender();
                    return false;

                case "exit":

                    return false;

                default:

                    return true;

so if i return false = program closes, true= program stays open


i never seen "Brake" so I'm not sure if this post is helpful at all.
 
@PinLui : In general, constructors should be used for initializing objects, not for doing operations or lots of work. The way you've written your code in post #9 has the constructors doing all the work.
 
C#:
        public static bool programRunning = true;
        static void Main(string[] args)
        {
            Thread RunApp = new Thread(() => RunOptions());
            RunApp.Start();
        }
        private static void RunOptions()
        {
            while (programRunning)
            {
                Utility myUtility = new Utility();
                myUtility.WelcomeUser();
                myUtility.PrintMainMenu();
                myUtility.MainMenuLogic();
            }
        }

        class Utility
        {
            public void WelcomeUser()
            {
                Console.WriteLine("\nWelcome to the hourly scheduler");
                Console.WriteLine("*******************************");
            }

            public void PrintMainMenu()
            {
                Console.WriteLine("\nMain Menu");
                Console.WriteLine("*********");
                Console.WriteLine("\n(a) Add appointment/s");
                Console.WriteLine("(r) Remove appointment/s");
                Console.WriteLine("(e) Edit appointment/s");
                Console.WriteLine("(v) View appointment/s");
                Console.WriteLine("(q) quit");
            }

            public void MainMenuLogic()
            {
                string userChoice = Console.ReadLine();
                char firstLetter = userChoice[0];
                switch (firstLetter)
                {
                    case 'a':
                        AddAppointment();
                        break;

                    case 'r':
                        RemoveAppointment();
                        break;

                    case 'e':
                        EditAppointment();
                        break;

                    case 'v':
                        ViewAppointments();
                        break;

                    case 'q':
                        //quit loop
                        programRunning = false;
                        break;
                }
            }
        }
C#:
        static void Main(string[] args)
        {
            Thread RunApp = new Thread(() => RunOptions());
            RunApp.Start();
        }
        private static void RunOptions()
        {
            Utility myUtility = new Utility();
            do
            {
                /* Does these until it reaches the while */
                myUtility.WelcomeUser();
                myUtility.PrintMainMenu();
            }
            while (myUtility.MainMenuLogic()); /* While running, if myUtility.MainMenuLogic() returns false, it exits */
        }

        class Utility
        {
            public void WelcomeUser()
            {
                Console.WriteLine("\nWelcome to the hourly scheduler");
                Console.WriteLine("*******************************");
            }

            public void PrintMainMenu()
            {
                Console.WriteLine("\nMain Menu");
                Console.WriteLine("*********");
                Console.WriteLine("\n(a) Add appointment/s");
                Console.WriteLine("(r) Remove appointment/s");
                Console.WriteLine("(e) Edit appointment/s");
                Console.WriteLine("(v) View appointment/s");
                Console.WriteLine("(q) quit");
            }

            public bool MainMenuLogic()
            {
                string userChoice = Console.ReadLine();
                char firstLetter = userChoice[0];
                switch (firstLetter)
                {
                    case 'a':
                        AddAppointment();
                        break;

                    case 'r':
                        RemoveAppointment();
                        break;

                    case 'e':
                        EditAppointment();
                        break;

                    case 'v':
                        ViewAppointments();
                        break;

                    case 'q':
                        //quit loop
                        return false;
                }
                return true;
            }
        }
You may ask why the thread. Nothing more than preference. I run everything on new threads. If you don't want it, remove it with RunOptions();
 
Sheepings

what is "If you are in another thread " is that just if you using an object that is instantiated from another class? never seen this "thread" before
 
Read the threading link in my signature. It explains everything you need to know... then read the linked pages also.

Simply to answer your question, no. It's not.

If you don't want to use the thread, then replace this :
C#:
            Thread RunApp = new Thread(() => RunOptions());
            RunApp.Start();
With : RunOptions();

In general, It's a principle I follow. When I am calling any code from the UI, such as a button, or even in console apps. I run everything on new threads. Not everyone agrees with doing it, but In my opinion its a good habit to develop for when you are dealing with UI on forms etc. Where heavy running processes are run on non user interface threads. Basically, when you run a timely execution on your UI thread it locks it up and makes your UI non responsive while the lengthly execution finishes. Running lengthily time consuming operations are always best run on a new thread, as this avoids locking up your UI. So for me, it's practically instinctual to introduce threading, and it doesn't do any harm. For the most part, you don't need it here. But its a habit worth developing.
 
Read the threading link in my signature. It explains everything you need to know... then read the linked pages also.

Simply to answer your question, no. It's not.

If you don't want to use the thread, then replace this :
C#:
            Thread RunApp = new Thread(() => RunOptions());
            RunApp.Start();
With : RunOptions();

In general, It's a principle I follow. When I am calling any code from the UI, such as a button, or even in console apps. I run everything on new threads. Not everyone agrees with doing it, but In my opinion its a good habit to develop for when you are dealing with UI on forms etc. Where heavy running processes are run on non user interface threads. Basically, when you run a timely execution on your UI thread it locks it up and makes your UI non responsive while the lengthly execution finishes. Running lengthily time consuming operations are always best run on a new thread, as this avoids locking up your UI. So for me, it's practically instinctual to introduce threading, and it doesn't do any harm. For the most part, you don't need it here. But its a habit worth developing.

Do you create front ends for your programs using WPF? Should I still bother with that?
 
Back
Top Bottom