C#Discord Bot cmd窗口关闭

时间:2018-10-17 19:29:05

标签: c# cmd discord

有一个非常简单的问题,但似乎无法在任何地方找到答案。我已经在C#中为Discord编写了一个bot,在进行调试或从debug文件夹运行时,一切正常。现在,我已经完成了所有功能,但是当我进行发行版本构建并运行.exe时,命令提示符窗口将闪烁并再次关闭。我了解这是代码完成执行时的常规行为,在这种情况下,代码应该具有这种行为。但是,这是机器人在等待命令,我需要该应用程序继续运行并监听命令。

我用作程序主程序的代码是:

static void Main(string[] args) => new Program().StartAsync().GetAwaiter().GetResult();

我相信问题一定在这里。

预先感谢

1 个答案:

答案 0 :(得分:1)

您应将代码封装在Main()方法中,并循环等待来自Discord的某种形式的quit命令。像这样:

static void Main(string[] args) 
{  
    bool quitRequested = false;

    while (!quitRequested)
    {
        // In your program you need it to receive a "quit" command from discord and
        // return a bool that is set to "true" when the "quit" command is received.
        quitRequested = new Program().StartAsync().GetAwaiter().GetResult();
    }
}