WPF - 使用按钮停止调度器计时器

问题描述

我有这个 WPF 应用程序。开始按钮启动调度员计时器来执行几个连续的任务。每项任务都可能非常耗时。我想要的是在当前任务花费太长时间(或实际挂起)时手动停止调度程序计时器 理论上,调度程序计时器每 500 毫秒滴答一次并更新停止按钮。 但是,正在处理的当前任务会锁定调度程序,直到当前任务完成。当当前任务结束时,停止按钮现在处于活动状态并生效,但不是实时的。 在调度程序计时器上尝试了不同的优先级,但没有成功。 我也尝试过异步任务和线程,但没有成功。

如何让停止按钮在用户界面中具有最高的响应优先级?

        // GLOBAL VARIABLES
        public bool fullStop = false;
        public int numTest = 0;
        public bool testActive = false;
        public string resultIndividualTest = null;
        
        // MAIN
        public MainWindow()
        { 
            InitializeComponent();
        }
        
        // UI COMPONENTS (Start and Stop Buttons),AND METHODS
        private void BStart_Click(object sender,RoutedEventArgs e)
        {
            // init counter
            InitCounterThread();
            // reset global variables
            numTest = 0; fullStop = false;
            // Init dispatcherTimer every 0.5 second with Priority "Render"
            timerTests = new dispatcherTimer(dispatcherPriority.Render); // Render
            timerTests.Interval = new TimeSpan(0,500); //500 ms
            timerTests.Tick += new EventHandler(TimerTests_Tick);
            timerTests.Start();
        }
        
        private void BStop_Click (object sender,RoutedEventArgs e)
        {
            fullStop = true;
            // stop dispatcherTimer
            timerTests.Stop();
            // stop counter
            StopCounter();
        }
        
        private CancellationTokenSource ts;
        private void TimerTests_Tick(object sender,EventArgs e)
        {
            // SET FOCUS AND REFRESH STOP BUTTON
            BStop.Focus();
            BStop.Refresh();
            // UPDATE LAYOUT UI
            this.UpdateLayout();
                                                        
            if (fullStop == false && testActive == false
            && numTest < MAX && resultIndividualTest == null){
                // next test (FROM 1 TO MAX)
                numTest++;
                // Now test will be activated until it finishes …
                testActive = true;
                ts = new CancellationTokenSource();
                CancellationToken ct = ts.Token;
                Task.Factory.StartNew( () => this.dispatcher.Invoke( 
                () => resultIndividualTest = BTestClick(numTest)),ct);            
            }
            else if (resultIndividualTest != null){
                // individual test (task) has finished with result no null
                resultIndividualTest = null;
                testActive = false;
                ts.Cancel();
            }
            else if (numTest >= MAX || fullStop == true){
                // stop dispatcherTimer
                timerTests.Stop();
                // stop counter
                StopCounter();
            }    
            else{
                // the timer continues …
            }
        }
        
        public string BTestClick(int numTest){
            /* This method does an individual test */
            string result = ""; bool r = false;
            If (numTest == 1) {r = routine1();}
            If (numTest == 2) {r = routine2();}
            … … …
            If (r == true) {result = "OK";}
            else {result = "ERROR"}
            return result;
        }
        
        public bool routine1(){
            bool result = false;
            // ... ... this may take very long (from 5 to 40 seconds)
            return result;
        }
        
        public bool routine2(){
            bool result = false;
            // ... ... this may take very long (from 5 to 40 seconds)
            return result;
        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)