简体   繁体   English

使用 console.readline 保持进程存活时如何读取控制台输入?

[英]How to read console input when using console.readline to keep process alive as well?

I'm learning/experimenting with SpotifyAPI.Net DotNetCore console application and have trouble reading user input from the console.我正在学习/体验 SpotifyAPI.Net DotNetCore 控制台应用程序,并且无法从控制台读取用户输入。 I think the console.readline() in the main thread (used for keeping the console app alive) takes precedence over the one i have in one of my methods.我认为主线程中的 console.readline() (用于保持控制台应用程序处于活动状态)优先于我在其中一种方法中使用的那个。 How do i get console input when having it keep the main thread alive at the same time?让主线程同时保持活动状态时如何获取控制台输入?

I've been searching about this for days but all I can find is questions on how to keep console apps alive.我已经搜索了好几天了,但我能找到的只是关于如何让控制台应用程序保持活力的问题。

I'm beginning to think I'm better off trying to use some simple UI?我开始认为我最好尝试使用一些简单的用户界面?

in my main method:在我的主要方法中:

AuthorizationCodeAuth auth =
                new AuthorizationCodeAuth(_clientId, _secretId, "http://localhost:4002", "http://localhost:4002",
                    Scope.PlaylistReadPrivate | Scope.PlaylistReadCollaborative);
            auth.AuthReceived += AuthOnAuthReceived;
            auth.Start();
            auth.OpenBrowser();
            Console.ReadLine();
            auth.Stop(0);

and then AuthOnAuthReceived is:然后 AuthOnAuthReceived 是:

        {
            AuthorizationCodeAuth auth = (AuthorizationCodeAuth) sender;
            auth.Stop();



            Token token = await auth.ExchangeCode(payload.Code);
            SpotifyWebAPI api = new SpotifyWebAPI
            {
                AccessToken = token.AccessToken,
                TokenType = token.TokenType
            };
            PrintUsefulData(api);
        }

PrintusefulData then does some things is where I want to grab some user console input with basic shit like: var input = Console.ReadLine(); PrintusefulData 然后做一些事情是我想用基本的狗屎来获取一些用户控制台输入: var input = Console.ReadLine();

But when debugging it never gets beyond this line of code because the program terminates because the readline in the main thread gets input and then it terminates.但是在调试时它永远不会超出这行代码,因为程序终止是因为主线程中的 readline 得到输入然后终止。

I would expect there to be some way to keep a console program alive while also getting some input from the user in the console.我希望有某种方法可以使控制台程序保持活动状态,同时还能从控制台中的用户那里获得一些输入。 But all my variations just end up terminating due to the userinput and never reaching the code I want to use for reading and processing the input.但是由于用户输入,我的所有变体最终都会终止,并且永远不会到达我想要用于读取和处理输入的代码。

I'm guessing I'm being a total newbie and missing something very obvious.我猜我是一个完全的新手,并且遗漏了一些非常明显的东西。

You could have a look at synchronization primitives , eg the ManualResetEvent .您可以查看同步原语,例如ManualResetEvent This will allow you to use WaitOne instead ReadLine to wait for the callback to be executed in your main method.这将允许您使用WaitOne而不是ReadLine来等待回调在您的main方法中执行。 WaitOne will block until Set is called, which you can do in AuthOnAuthReceive . WaitOne将阻塞直到调用Set ,您可以在AuthOnAuthReceive中执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM