在Tron游戏中使用Graphics时出现InvalidOperationException。 [英] InvalidOperationException when using Graphics in a Tron game.

查看:84
本文介绍了在Tron游戏中使用Graphics时出现InvalidOperationException。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将一些文本写入图片框时,我得到一个AccessViolationException,然后我在AccessViolationException之后反复出现InvalidOperationExceptions。

AccessViolationException:

I get a AccessViolationException when I try to write some text to a picturebox, then I get recurring InvalidOperationExceptions following the AccessViolationException.
AccessViolationException:

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at System.Drawing.SafeNativeMethods.Gdip.GdipDrawString(HandleRef graphics, String textString, Int32 length, HandleRef font, GPRECTF& layoutRect, HandleRef stringFormat, HandleRef brush)
   at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
   at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y)
   at Tron.Form1.Reset() in C:\Users\...\Desktop\Tron\Form1.vb:line 81



InvalidOperationExceptions(所有这些)是相同的)


InvalidOperationExceptions (All of them are the same)

System.InvalidOperationException: Object is currently in use elsewhere.
   at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
   at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
   at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y)
   at Tron.Form1.Reset() in C:\Users\...\Desktop\Tron\Form1.vb:line 81



给出错误的代码:


Code that's giving the error:

SyncLock PictureBox1
    PictureBox1.Image = FillBitmap(Color.Black)
    Dim img As Image = PictureBox1.Image
    Using g As Graphics = Graphics.FromImage(img)
        g.DrawString("TRON", Form1.TronFont, Brushes.White, (PictureBox1.Width / 2) - 250, (PictureBox1.Height / 2) - 50)
        g.DrawString("- P to play", New Font(Form1.TronFont.FontFamily, 18), Brushes.White, (PictureBox1.Width / 2) - 250, (PictureBox1.Height / 2) - 20)
        g.DrawString("- H to see highscores", New Font(Form1.TronFont.FontFamily, 18), Brushes.White, (PictureBox1.Width / 2) - 250, PictureBox1.Height / 2)
        g.Dispose()
    End Using
    PictureBox1.Image = img
End SyncLock



第81行:


Line 81:

g.DrawString("TRON", Form1.TronFont, Brushes.White, (PictureBox1.Width / 2) - 250, (PictureBox1.Height / 2) - 50)



我知道我可能在错误的上下文中使用SyncLock或者以错误的方式。

我不知道如何解决这个错误。


I know I am probably using SyncLock in the wrong context or in the wrong way.
I don't know how to fix this error.

推荐答案

你甚至知道SyncLock的作用吗?它只适用于你有多个线程试图访问相同的资源,一次只能由一个线程访问该资源。



如果你是试图从多个线程操纵图形对象,你遇到了麻烦。你不能触摸与UI(启动)线程相关的任何UI相关的任何UI。



此外,你甚至不需要在你的表单中绘制PictureBox。您可以直接在表单表面或Panel控件上绘制。 PictureBox只是一个快速编码显示图像的容器。它不是为游戏而设计的,坦率地说,在屏幕上绘制图像会增加开销。
Do you even know what SyncLock does? It only applies if you're got multiple threads trying to access the same resource where that resource must be accessed by only one thread at a time.

If you're trying to manipulate graphics objects from multiple threads, you're in trouble. You can NOT touch anything UI related from anything other that the UI (startup) thread.

Also, you don't even need a PictureBox to draw in your forms. You can draw directly on the form surface, or on a Panel control. A PictureBox is just a container to quickly code up showing an image. It was no designed for games and is frankly, ADDING overhead to you drawing an image on screen.


这篇关于在Tron游戏中使用Graphics时出现InvalidOperationException。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆