Search code examples
c#multithreadingreal-time

Display string value real time


I'm writing a console program, and my code looks like this:

Configuration.cs

public static class Configuration
{
    public static string Message = "";
}

Menu.cs

class Menu
{
    public static void showMenu()
    {
        Console.Clear();
        Console.WriteLine("1: SOMETHING");
        Console.WriteLine("2: SOMETHING");
        Console.WriteLine("3: SOMETHING");
        Console.WriteLine("SYSTEM MSG: " + Configuration.Message);
        Console.Write("INPUT: ");
    }
}

Program.cs

...
static void Main(string[] args)
{
    ...
    int choice;

    while(true)
    {
        Menu.showMenu();
        choice = Convert.ToInt32(Console.ReadLine());

        switch(choice)
        {
            case 1:
            Configuration.Message = "HELLO!";
            break;

            case 2:
            Configuration.Message = "HI!";
            break;

            case 3:
            Configuration.Message = "WHAT?!";
            break;
        }
    }
}
...

For now, when I change Configuration.Message, it will display on the menu because the showMenu method clears the console and show the string again.

But what I want to make is without Clear method, I want to show Configuration.Message for real time. I was thinking that using Timer and refresh the menu every second, but it is not efficient (feels like cheating). How can I do this?


Solution

  • I would suggest that if functionality like this is necessary it would be better to look at a UI technology like WPF. But, that being said, maybe this would do what you wanted. Showing the menu each time you set the Message. I do not think when using the console there is a way to bind a variable to some message on the console.

        public static class Configuration
        {
            private static string _message;
            public static string Message 
            { 
                get
                {
                    return _message;
                }
                set
                {
                    _message = value;
                    Menu.showMenu();
                }
            }
        }
    

    Edit: to implement this using a property changed event you could do something like this. Note, I am not implementing INotifyPropertyChanged as to keep the static class.

    class Program
        {
            static void Main(string[] args)
            {
                Configuration.PropertyChanged += (sender, e) =>
                {
                    Menu.showMenu();
                };
                int choice;
    
                while (true)
                {
                    Menu.showMenu();
                    choice = Convert.ToInt32(Console.ReadLine());
    
                    switch (choice)
                    {
                        case 1:
                            Configuration.Message = "HELLO!";
                            break;
    
                        case 2:
                            Configuration.Message = "HI!";
                            break;
    
                        case 3:
                            Configuration.Message = "WHAT?!";
                            break;
                    }
                }
            }
        }
    
        class Menu
        {
            public static void showMenu()
            {
                Console.Clear();
                Console.WriteLine("1: SOMETHING");
                Console.WriteLine("2: SOMETHING");
                Console.WriteLine("3: SOMETHING");
                Console.WriteLine("SYSTEM MSG: " + Configuration.Message);
                Console.Write("INPUT: ");
            }
        }
    
        public static class Configuration 
        {
            public static event PropertyChangedEventHandler PropertyChanged;
    
            private static string _message;
            public static string Message
            {
                get
                {
                    return _message;
                }
                set
                {
                    if (value != _message)
                    {
                        _message = value;
                        NotifyPropertyChanged(property: _message);
                    }
                }
            }
    
            private static void NotifyPropertyChanged(object property, String propertyName = "")
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(property, new PropertyChangedEventArgs(propertyName));
                }
            }
        }