[英]How can I create a console application that utlilitize async/await
I'm creating a weather app that polls temperature from a service I've made: 我正在创建一个气象应用,该气象应用会根据我提供的服务来查询温度:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Weather
{
class Program
{
static BackgroundWorker bgw;
static void Main(string[] args)
{
bgw = new BackgroundWorker();
bgw.DoWork += bgw_DoWork;
bgw.RunWorkerAsync();
}
static async void bgw_DoWork(object sender, DoWorkEventArgs e)
{
Weather bot = new Weather();
if (bot.IsRunning)
{
await bot.Update();
}
}
}
public class Weather
{
public bool IsRunning { get; set; }
private DateTime lastUpdated;
public Weather()
{
IsRunning = true;
lastUpdated = DateTime.Now.AddDays(-1);
}
public async Task<bool> Update()
{
if (lastUpdated < DateTime.Now)
{
lastUpdated = DateTime.Now.AddSeconds(30);
// temperature
double value = await GetLatestValue("New York");
}
return true;
}
private async Task<double> GetLatestValue(string city)
{
string url = "http://www" + city;
var client = new WebClient();
string data = await client.DownloadStringTaskAsync(url);
return 4.3;
}
}
}
The problem here is that it does not seem to work? 这里的问题是它似乎不起作用? The GetLatesValue function is just jibberish, will just return 4.3 for testing purposes.
GetLatesValue函数只是一点点杂乱无章,仅返回4.3进行测试。
What happens is that on await GetLatestValue
the console application just quits. 发生的情况是,在
await GetLatestValue
,控制台应用程序刚刚退出。
The problem is simpler than you might think: you are running a BackgroundWorker
, which basically wraps a thread that has .IsBackground = true
. 这个问题比您想象的要简单:您正在运行
BackgroundWorker
,它基本上包装了一个具有.IsBackground = true
的线程。 Such threads will not keep a process alive - they will be shut down automatically when the process exits. 这样的线程将无法使进程保持活动状态-当进程退出时,它们将自动关闭。 The process will exit when all non -background threads are completed.
所有非后台线程完成后,该过程将退出。
Your Main
method starts the BackgroundWorker
, but then does nothing else - Main
exits, and the application is complete. 您的
Main
方法将启动BackgroundWorker
,但随后不执行其他操作Main
退出,应用程序完成。 The BackgroundWorker
is then shut down at whatever point it's reached. 然后,
BackgroundWorker
会在到达任何时候关闭。 There's nothing wrong with the code it's running - but the app is shutting down without letting it complete. 它正在运行的代码没有任何问题-但该应用程序正在关闭而没有完成。
EDIT: if you want to test this, simply put a Console.ReadLine()
at the end of your Main
- it'll keep the application alive until you press Enter, and so you should see your thread run until you do. 编辑:如果您要测试此,只需在您的
Main
的末尾放置一个Console.ReadLine()
-它可以使应用程序保持活动状态,直到您按Enter键为止,因此您应该看到线程正在运行,直到您这样做为止。
In addition to Dan Puzey's answer , there's not much sense in assigning an async void
method as an event handler for BackgroundWorker
, in the first place. 除了Dan Puzey的答案外 ,首先将
async void
方法分配为BackgroundWorker
的事件处理程序没有多大意义。
Your worker method bgw_DoWork
will return and the background thread will be finished as soon as the execution point hits the first await
inside bgw_DoWork
. 一旦执行点
bgw_DoWork
内部的第一个await
位置,您的辅助方法bgw_DoWork
将返回并且后台线程将完成。 The bot.Update
task most likely still will be pending at that point. 此时
bot.Update
任务很可能仍将挂起。
You don't need a BackgroundWorker
here. 您在这里不需要
BackgroundWorker
。 The code can be as simple as this: 代码可以像这样简单:
static void Main(string[] args)
{
DoWorkAsync().Wait();
}
static async Task DoWorkAsync()
{
Weather bot = new Weather();
if (bot.IsRunning)
{
await bot.Update();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.