c# - C# 中 BackgroundWorker 的性能问题

标签 c# .net wpf backgroundworker

我在 C# 中遇到 BackgroundWorker 性能的奇怪问题。我有一个应用程序,除其他外,它使用 BackgroundWorker 来执行某些任务。基本上任务如下:

public void simulate(Image imgSimulator)
        {
            simulador = new Simulator(imgSimulator);
            simulador.setBackground(0);
            Constants.finishSimulation = false;
            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += run;
            bw.RunWorkerAsync(imgSimulator);
        }

public void run(object sender, DoWorkEventArgs e)
        {
            Image imgSimulator = (Image)e.Argument;
            bool clear;
            foreach (Program p in programs)
            {
                Resource r = p.getResource(0);
                clear = true;
                if (r is Text)
                {
                    Text t = (Text)r;
                    clear = t.getClearPrev() == 1;
                }
                if (!clear)
                {
                    simulador.setBackground(FontBitmap.COLOR_BLACK);
                }
                p.execute(simulador, imgSimulator);
                if (Constants.finishSimulation)
                {
                    break;
                }
            }

        }

上面代码中的主要函数是execute:

public void execute(Simulator simulador, System.Windows.Controls.Image imgSimulator)
        {
            long now = DateTime.Now.Ticks / 10000;
            long current = DateTime.Now.Ticks / 10000;
            while (true)
            {
                current = DateTime.Now.Ticks / 10000;
                if (current - now >= 1)
                {
                    App.Current.Dispatcher.Invoke((Action)(() =>
                    {
                        ((MainWindow)System.Windows.Application.Current.MainWindow).Title = "" + Constants.index++;

                    }));
                    now = DateTime.Now.Ticks / 10000;
                }
            }
        }

我修改了执行函数以进行调试,现在它更改了主窗口标题。

问题是应用程序在我的电脑上运行正常,但我在另一台电脑上试过,标题没有以相同的速度更新。

这里有一些数据(我为做这个测试唯一改变的是数字 10000)

如果我在我的电脑上将 10000 更改为 1000000,应用程序需要 30 秒才能达到 300(在窗口标题栏中),而另一台电脑也会发生同样的情况。 如果我在我的电脑中将 10000 更改为 100000,应用程序需要 30 秒才能达到 3000(在窗口标题栏中),但在另一台电脑中需要 47 秒才能达到 3000

我注意到的另一件事是,如果我打开另一个 C# 应用程序 (WPF) 并将鼠标移到它们的控件上(或聚焦一个文本框),该应用程序将正确运行(以与它在我的电脑中运行的速度相同的速度运行) .

我的电脑和其他电脑的唯一区别是我安装了 Visual Studio 2013。

可能是什么问题?

谢谢。

最佳答案

   if (current - now >= 1)

这并没有达到您希望的效果,DateTime.Now 不会每毫秒更新一次。默认情况下,它每秒更新 64 次,每 15.625 毫秒更新一次。因此,您的 Constants.index 每秒不能超过 64。

但是其他进程和驱动程序可以通过调用 timeBeginPeriod() 来更改该更新率。例如,浏览器很容易将其更改为 10 毫秒,这是动画 gif 的一个快乐数字。现在,您将每秒递增 Constants.index 一百次。

这正是您所看到的:15.625/10 * 30 秒 = 47 秒。

您可以通过在提升的命令提示符下运行 powercfg/energy 来查看机器上的事件速率。生成的报告在“平台计时器分辨率”标题下显示数字。

必须避免依赖更新率。

关于c# - C# 中 BackgroundWorker 的性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34339998/

相关文章:

c# - ElementHost - 调用 Show 方法时出现黑色背景

c# - 如何使用 MVVM 模式从数据库中填充 ListView?

c# - 有效地将项目添加到列表<类>中并行 C#

c# - C# 中的 ref 参数

c# - FullTextSqlQuery 的搜索范围是什么

c# - 使用/不使用相对路径在 C# 中写入文件

c# - 在其阻塞版本中使用方法的异步实现

c# - 如何找出窗口标题的字体大小?

c# - Service Fabric Actor 服务依赖注入(inject)和 Actor 事件

c# 远程 powershell 创建 AD/Exchange 用户帐户并修改