我创建了一个单独的线程,其中我创建了GLControl(engineViewport)并循环:
线程子:
keypad = {
'A': 2, 'B': 2, 'C': 2,
'D': 3, 'E': 3, 'F': 3,
'G': 4, 'H': 4, 'I': 4,
'J': 5, 'K': 5, 'L': 5,
'M': 6, 'N': 6, 'O': 6,
'P': 7, 'Q': 7, 'R': 7,
'S': 7, 'T': 8, 'U': 8,
'V': 8, 'W': 9, 'X': 9,
'Y': 9, 'Z': 9
}
phone_num = input('Enter a phone number: ')
phone_as_digits = ""
for char in phone_num:
phone_as_digits += str(keypad[char.upper()]) if char.isalpha() else char
print(phone_as_digits)
在同一个线程上我做了所有的计算,在Paint事件中我做了所有GL的东西(不多,简单的低polly,一个纹理,但它需要刷新非常快)。
绘制事件子:
engineViewport = New OpenTK.GLControl()
Do Until engineViewport.Created = True
Thread.Yield()
Loop
AddHandler engineViewport.Paint, AddressOf engineVievport_Paint
Dim frameTickDurationMS As Long = 1000 / FPS
Dim frameTickTimeMS As New Stopwatch()
Dim prevTickTimeMS As Long = 0
frameTickTimeMS.Start()
Do
'some simple calculations here
'irrevelant to this situaltion
Do While (frameTickTimeMS.ElapsedMilliseconds - prevTickTimeMS) < frameTickDurationMS
'wait for accurate time for rendering next frame / FPS based
Thread.Yield()
Loop
'invalidate GLControl to render OpenGL frame
If engineViewport.IsIdle Then
engineViewport.Invalidate()
End If
prevTickTimeMS = frameTickTimeMS.ElapsedMilliseconds
Loop
工作正常,但在某些时候(75FPS约为最大值),则UI开始滞后(不会实时响应事件)。处理器负载总量为10-12%(甚至分布在核心上)所以这不是cpu bottelneck,GPU负载也是5-10%... GLControl本身很好(sucesffuly测试120FPS),流畅地绘制。只有UI才是问题。
我怀疑所有UI和Paint事件都在同一个线程上运行,这会导致UI变得迟钝。
有没有办法将GLControl的Paint事件推送到一个单独的进程?基本上使整个链形式Invalidate到Paint事件与主UI线程分开。我不需要PaintEventArgs所以如果有可能我可以从我创建GLControl的线程发送GL命令,但这不起作用。