VB.Net windows窗体图形在windows 10上运行缓慢

VB.Net windows窗体图形在windows 10上运行缓慢,vb.net,Vb.net,在旧的Windows7计算机上使用VB2010,我编写了一些Win表单代码,以图形方式模拟静态,运行速度很快 当使用VB2010在较新的Windows 10机器上运行相同的代码时,运行速度非常慢(大约是速度的十分之一)。使用VB2010,它在我的Windows 10笔记本电脑上运行缓慢。我还使用VS2017在我的两台Windows 10机器上试用过,但速度仍然很慢 Imports System.Drawing Imports System.Math Public Class Form1

在旧的Windows7计算机上使用VB2010,我编写了一些Win表单代码,以图形方式模拟静态,运行速度很快

当使用VB2010在较新的Windows 10机器上运行相同的代码时,运行速度非常慢(大约是速度的十分之一)。使用VB2010,它在我的Windows 10笔记本电脑上运行缓慢。我还使用VS2017在我的两台Windows 10机器上试用过,但速度仍然很慢

Imports System.Drawing
Imports System.Math

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Me.Show()
        DoStatic()

    End Sub

    Private Sub DoStatic()

        Dim lineGreen As New Pen(Color.LightGreen)
        Dim lineBlack As New Pen(Color.Black)
        Dim formGraphics As Graphics

        Dim staticAmplitudeUpper As Integer
        Dim staticAmplitudeLower As Integer
        Dim x As Integer
        Dim y As Integer

        formGraphics = Me.CreateGraphics()

        y = 200

        For duration As Integer = 1 To 10

            For x = 20 To 1020

                staticAmplitudeUpper = CInt(Ceiling(Rnd() * 5)) + 1
                staticAmplitudeLower = CInt(Ceiling(Rnd() * 5)) + 1

                formGraphics.DrawLine(lineBlack, x, y - 6, x, y + 6)
                formGraphics.DrawLine(lineGreen, x, y, x, 200 - staticAmplitudeUpper)
                formGraphics.DrawLine(lineGreen, x, y, x, 200 + staticAmplitudeLower)

            Next

        Next


    End Sub

从一开始就错了。尽管为了简单起见有很多这样做的例子,但您永远不应该调用
CreateGraphics
。您需要将绘图代码移动到
Paint
事件处理程序或
OnPaint
方法,并使用提供的
图形
对象。此外,无需为标准
颜色
值创建
画笔
对象。
Pens
类已经提供了它们。我不能100%确定您的代码应该做什么,因为您已经决定不提供注释是一个好策略。不是。这段代码应该在执行时改变屏幕上显示的内容还是什么?你是否真的做过任何计时,以查看实际占用的时间?这是一个CPU猪。您的代码将使用尽可能多的CPU,因此,它的速度也取决于它能获得多少CPU功率(比平时更多)。正如jmchilinney所说,应该将其移动到
Paint
事件,而不是持续时间
循环,使用调用
Me.Invalidate()
System.Windows.Forms.Timer
来再次引发
Paint
事件,从而更新动画。