在WPF中执行SQL查询时在UI上显示计时器

在WPF中执行SQL查询时在UI上显示计时器,wpf,timer,Wpf,Timer,当我点击“开始”按钮时,我的SQL查询开始执行。我想在单击开始按钮时启动计时器,并在UI中完成查询执行时停止计时器。请建议我如何在WPF中执行计时器。尝试使用BackgroundWorker类: BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += (o, ea) => { //Work to be done i.e executing the SQL query }; worker

当我点击“开始”按钮时,我的SQL查询开始执行。我想在单击开始按钮时启动计时器,并在UI中完成查询执行时停止计时器。请建议我如何在WPF中执行计时器。

尝试使用
BackgroundWorker
类:

 BackgroundWorker worker = new BackgroundWorker();
  worker.DoWork += (o, ea) =>
  {
    //Work to be done i.e executing the SQL query
  };

  worker.RunWorkerCompleted += (o, ea) =>
  {
    //Code for when the job has complete i.e stopping the timer
  };

  //Start the timer here
  worker.RunWorkerAsync();
这将在执行代码时保持UI的响应性


希望这有帮助

您是否遵循MVVM?,并且在计时器初始化或显示计时器时遇到问题?你在使用BackgroundWorker吗?看看BackgroundWorker类是的,我在使用MVVM和BackgroundWorker