Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在不暂停或中断程序的情况下,以多线程方式读取c#中的行?_C#_Multithreading_Console Application_Readline_Steambot - Fatal编程技术网

如何在不暂停或中断程序的情况下,以多线程方式读取c#中的行?

如何在不暂停或中断程序的情况下,以多线程方式读取c#中的行?,c#,multithreading,console-application,readline,steambot,C#,Multithreading,Console Application,Readline,Steambot,我有一个循环的程序, 并希望在不中断或暂停程序的情况下实现实时人机交互 我想能够让它使用的程序已经运行的输入命令 基本上,如果我没有正确解释: -主程序在前台运行 -在后台查找输入运行的辅助无限循环 -在输入并点击返回键(ReadLine)时,字符串被保存并发送到另一个函数(例如解释器() -我已经准备好读取返回的输入(一个简单的循环),我不明白如何随时允许用户输入 编辑: 我知道堆栈溢出通常是为了帮助处理代码中的错误,但我知道它应该很简单,我只是不知道从哪里开始 编辑2: using Syst

我有一个循环的程序, 并希望在不中断或暂停程序的情况下实现实时人机交互

我想能够让它使用的程序已经运行的输入命令

基本上,如果我没有正确解释:

-主程序在前台运行

-在后台查找输入运行的辅助无限循环

-在输入并点击返回键(ReadLine)时,字符串被保存并发送到另一个函数(例如解释器()

-我已经准备好读取返回的输入(一个简单的循环),我不明白如何随时允许用户输入

编辑: 我知道堆栈溢出通常是为了帮助处理代码中的错误,但我知道它应该很简单,我只是不知道从哪里开始

编辑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();
    }
}
}
使用系统;
使用系统线程;
使用蒸汽发生器2;
使用蒸汽贸易;
使用System.Collections.Generic;
使用系统文本;
命名空间蒸汽机器人
{
公共课程
{
公共静态void Main(字符串[]事件参数)
{
LicenseGlobal.Seal.Initialize(“我的许可证密钥*请忽略此*”;
if(System.IO.File.Exists(“settings.json”))
{
Configuration config=Configuration.LoadConfiguration(“settings.json”);
Log mainLog=新日志(config.mainLog,null);
foreach(config.Bots中的Configuration.BotInfo信息)
{
mainLog.Info(“启动Bot”+Info.DisplayName+”);
新线程(()=>
{
int=0;
同时(碰撞<1000)
{
尝试
{
新建Bot(info,config.ApiKey,(Bot Bot,SteamID sid)=>
{
return(streambot.UserHandler)System.Activator.CreateInstance(Type.GetType(bot.BotControlClass)),新对象[]{
机器人程序,
希德
});
},假);
}
捕获(例外e)
{
mainLog.Error(“机器人出错:+e”);
崩溃++;
}
}
}).Start();
睡眠(5000);
}
}
其他的
{
Console.WriteLine(“配置文件不存在。请将“settings template.json”重命名为“settings.json”,并修改设置以匹配您的环境”);
}
Console.WriteLine(“你好世界”);
Console.ReadKey();
}
}
}

您的主线程应该是等待用户输入的线程。 大概是这样的:

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;
        }
    }
}

当不响应用户输入时,程序在做什么?听起来您正在编写REPL(读取、计算、打印循环),但在这种情况下,我认为不需要辅助线程。阅读后只需评估并打印即可。循环只是等待用户输入。
“我知道堆栈溢出通常是为了帮助解决代码中的错误,但我知道它应该很简单”
您仍然需要向我们展示代码,否则我们会像您一样不知所措。如果我们不需要想象代码的话,我们可以更容易地帮助您。可以,但是,首先,它来自一个开源项目,我正在修改以满足我的需要。将main添加到原始帖子中。我将尝试实现它!谢谢,我会告诉你它是怎么回事。它确实接受了它,但现在它在我的课堂上抛出了错误:/谢谢,它应该会起作用,但显然我还有很多工作要做