计时器组合的Application.DoEvents无法正常运行

在我的应用程序中,我正在处理许多数据。因此在处理数据时冻结。所以我有一个计时器。我在计时器的已过去事件中调用Application.DoEvents。代码在

下面
    class StoSCorrelation : Reports
    {
        double timer_Interval = 5;
        public System.Timers.Timer timer;
        public StoSCorrelation()
        {
            timer = new System.Timers.Timer(timer_Interval);
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.AutoReset = true;
            timer.Enabled = false;
        }
        //The below function freezes the screen
        public bool generateReport()
        {
            try
            {
                timer.Enabled = true;
                //deal with the data
                if (//some condition)
                {
                    timer.Enabled = false;
                    return true;
                }
                else
                {
                    timer.Enabled = false;
                    return false;
                }

            }
            catch (Exception msg)
            {
                //Log
                timer.Enabled = false;
                return false;
            }
        }
        void timer_Elapsed(object sender,ElapsedEventArgs e)
        {
            Application.DoEvents();
        }

因此,我希望屏幕不会冻结。但是仍然是冰冷的。我检查了消息框,以了解计时器是否在间隔中触发。似乎正在发生。但是屏幕仍然冻结。我想知道为什么吗?以及如何解决这个问题。我希望我的屏幕处于活动状态。 注意:线程未完成。我正在使用C#3.5,Windows 10 Pro,VS 2008。

st86591985 回答:计时器组合的Application.DoEvents无法正常运行

我怀疑您正在GUI线程中执行繁重的任务。

我不会使用计时器来保持GUI正常运行,因为计时器将在GUI线程中运行。 GUI将不响应Application.DoEvents();当它挂起时。 我宁愿在繁重的任务上使用多线程。

这可以帮助您入门: https://www.tutorialspoint.com/csharp/csharp_multithreading.htm

本文链接:https://www.f2er.com/3073357.html

大家都在问