简体   繁体   English

如何创建不结束但等待事件的控制台应用程序?

[英]How do I create a console app that does not end but wait for events?

I want to create a bot with the twitter stream API.我想用 twitter 流 API 创建一个机器人。 So basically an event gets triggered and I do something.所以基本上一个事件被触发,我做一些事情。

I want to let it run in a docker container in a linux-environment.我想让它在 linux 环境中的 docker 容器中运行。

The issue I have is that my console just closes itself.我遇到的问题是我的控制台会自行关闭。 Here is my code这是我的代码

public static async Task Main()
{
    var  credentials = new TwitterCredentials(consumerKey: "",
                                              consumerSecret: "",
                                              accessToken: "",
                                              accessTokenSecret: "");    
    var stream = Tweetinvi.Stream.CreateFilteredStream(credentials);

    stream.AddFollow(someUserId,
                     tweet =>
                     {
                         doSomething(tweet);
                     });

    stream.MatchingTweetReceived += (s, e) =>
    {
        doSomething(e.Tweet);
    };

    await stream.StartStreamMatchingAllConditionsAsync();
}

I wait for the Stream to be started but then the console closes itself.我等待 Stream 启动,但随后控制台自行关闭。 I'm not waiting for any events.我不是在等待任何事件。 How can I change this?我怎样才能改变这个?

It sounds like StartStreamMatchingAllConditionsAsync completes at some point, and this is causing your application to exit.听起来StartStreamMatchingAllConditionsAsync在某个时候完成,这会导致您的应用程序退出。

What you need is something that you can await on but will never complete.你需要的是你可以await但永远不会完成的东西。 Task.Delay can do this for you. Task.Delay可以为您做到这一点。 Just make this the last line of your function:只需将其作为函数的最后一行:

await Task.Delay(-1);

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

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