每5秒钟单击一次执行按钮,直到后台工作人员停止操作并关闭

问题描述

我已经创建了一个调用sql表的按钮,用户可以查看表的行。基本上,像

这样的查询
SELECT * FROM table1

,按钮的名称称为View

我还创建了以下后台工作程序,该后台工作程序执行了长时间运行的查询,持续了4分钟:

using System.Windows;
using System.ComponentModel;
using System.Threading;
using System;
using System.IO;
using System.Data.sqlClient;
using System.Windows.Input;

namespace TestEnvironment
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>

    public partial class ProgressBarTemplate : Window
    {
        private CreateProjectScreen _CreateProjectScreen;
        private LoginScreen _LoginScreen;

        public ProgressBarTemplate()
        {
            InitializeComponent();
        }

        public static int RuncalculationsMethod(string connectionstring,string foldername)
        {
            bool exists = Directory.Exists(foldername);

            if (!exists)
            {
                Directory.CreateDirectory(foldername);
            }

            try
            {
                using (sqlConnection sqlConnection = new sqlConnection(connectionstring))
                {
                    var calculations_query = "SELECT * FROM table1");

                    using sqlCommand sqlCommand = new sqlCommand(calculations_query,sqlConnection);

                    sqlConnection.open();

                    sqlCommand.CommandTimeout = 60 * 10;

                    int NumbderOfRecords = sqlCommand.ExecuteNonQuery();

                    return NumbderOfRecords;
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(),"Error",MessageBoxButton.OK,MessageBoxImage.Error);
                return -100;
            }
        }

        private void Window_ContentRendered(object sender,EventArgs e)
        {
            BackgroundWorker worker = new BackgroundWorker();
            
            worker.DoWork += worker_DoWork;
            worker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;

            worker.RunWorkerAsync();
        }

        void worker_DoWork(object sender,DoWorkEventArgs e)
        {
            int IsSuccessful = RuncalculationsMethod("Server=localhost;Database=DB_Name;Integrated Security=sspI",String.Format("C:\\folder_path\\"));
        }

        void BackgroundWorker_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
        {
            // This is called on the UI thread when the DoWork method completes
            // so it's a good place to hide busy indicators,or put clean up code

            try
            {
                this.Close();
                MessageBox.Show("DQ Calculations completed successfully","@R_658_4045@ion",MessageBoxImage.@R_658_4045@ion);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(),MessageBoxImage.Error);
            }
        }
    }
}

这里的琐碎情况是按钮View位于我的MainWindow上,而上面的代码位于名为ExecuteBGWorker()的第二个窗口中。

我想要实现的是使时间触发事件每5秒执行一次按钮View,直到后台工作人员停止。当工作人员停止时,{{1}中的View按钮也将停止自动单击。

下面是我如何从MainWindow调用ExecuteBGWorker()方法代码

MainWindow

当我单击“查看”按钮时

enter image description here

5秒后,再次单击sql表将显示更多记录

enter image description here

我显然不知道如何实现这一点。因此,请感谢我为发布问题所做的努力。我确实进行了搜索,发现了this question中的dispatcherTimer计时器功能。但是,我已经很长时间不熟悉C#了,所以我不确定如何实现它并在BG_Worker完成后关闭计时器。

解决方法

您应该将执行查询的代码移至类的方法,并只需使用计时器每五分钟调用一次此方法,例如:

System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += (s,e) =>
{
    //execute query
};
timer.Interval = 60000 * 5;
timer.Start();