如何将influxdb查询响应返回到Mainwindow类?

时间:2019-06-05 11:12:01

标签: c# wpf

我在异步任务中有一个对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());
   }
}

1 个答案:

答案 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被认为是不好的做法,并且可能会死锁。