我在异步任务中有一个对influxdb数据库的查询。我正在尝试将结果返回到MainWindow
类变量。有人可以帮我吗?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
string result= ConsultaBD().Result;
}
public async Task<string> ConsultaBD()
{
var influxDbClient = new InfluxDbClient("http://host:8086/", "user", "pass", InfluxDbVersion.v_1_3);
var query = "SELECT T_PV FROM TFA WHERE time >= '2019-05-21' and time < '2019-05-22' ";
var response = await influxDbClient.Client.QueryAsync(query, "dbName");
return (response.ToString());
}
}
答案 0 :(得分:1)
例如,您可以等待ConsultaBD()
事件处理程序中的async
方法,该方法在加载窗口时被调用:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += async (s, e) =>
{
string result = await ConsultaBD();
};
}
public async Task<string> ConsultaBD()
{
var influxDbClient = new InfluxDbClient("http://host:8086/", "user", "pass", InfluxDbVersion.v_1_3);
var query = "SELECT T_PV FROM TFA WHERE time >= '2019-05-21' and time < '2019-05-22' ";
var response = await influxDbClient.Client.QueryAsync(query, "dbName");
return (response.ToString());
}
}
在Result
方法上调用Wait()
或async
被认为是不好的做法,并且可能会死锁。