Pregunta

I have a program going in a loop, and would wish to allow for real time human interaction without breaking or pausing the program.

I would like to be able to have it use the input as a command for the program running already.

Basically, if I didn't explain it properly:

-main program runs in foreground.

-secondary infinite loop looking for input runs in background

-upon input and hitting the return key (ReadLine), the string saved and sent over to another function (e.g. Interpreter())

-I have everything already set up to read the returned input (a simple loop), I just cant comprehend how to allow user input at anytime.

Edit: I know stack overflow is generally for help on an error in code, but I know it should be simple, I just don't understand where to start.

Edit 2:

using System;
using System.Threading;
using SteamKit2;
using SteamTrade;
using System.Collections.Generic;
using System.Text;

namespace SteamBot
{
public class Program
{
    public static void Main(string[] EventArgs)
    {
        LicenseGlobal.Seal.Initialize("MY LICENSE KEY *please ignore this*");

        if (System.IO.File.Exists("settings.json"))
        {

            Configuration config = Configuration.LoadConfiguration("settings.json");
            Log mainLog = new Log(config.MainLog, null);
            foreach (Configuration.BotInfo info in config.Bots)
            {
                mainLog.Info("Launching Bot " + info.DisplayName + "...");
                new Thread(() =>
                {


                    int crashes = 0;
                    while (crashes < 1000)
                    {
                        try
                        {
                            new Bot(info, config.ApiKey, (Bot bot, SteamID sid) =>
                            {

                                return (SteamBot.UserHandler)System.Activator.CreateInstance(Type.GetType(bot.BotControlClass), new object[] {
                                        bot,
                                        sid
                                    });
                            }, false);

                        }
                        catch (Exception e)
                        {
                            mainLog.Error("Error With Bot: " + e);
                            crashes++;
                        }
                    }
                }).Start();
                Thread.Sleep(5000);
            }
        }
        else
        {
            Console.WriteLine("Configuration File Does not exist. Please rename 'settings-template.json' to 'settings.json' and modify the settings to match your environment");
        }
        Console.WriteLine("Hello World");
        Console.ReadKey();
    }
}
}
¿Fue útil?

Solución

Your main thread should be the one that's waiting for the user input. Something like this:

using System;
using System.Threading;

namespace Input
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            // This is to stop the worker thread
            var workerShouldStop = false;

            var worker = new Thread(() => 
            {
                // do work
                while (!workerShouldStop) {
                    Thread.Sleep(1000);
                    Console.WriteLine("doing things ...");
                };
            });

            worker.Start();

            string input;
            do 
            {
                Console.Write ("Your input (enter to quit):");
                input = Console.ReadLine();
                Console.WriteLine("input is:" + input);
            } while(!String.IsNullOrWhiteSpace(input));

            // Stop the worker thread
            workerShouldStop = true;
        }
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top