简体   繁体   English

几秒钟后运行OpenTK.Graphics.GraphicsContextException

[英]OpenTK.Graphics.GraphicsContextException after a few seconds of running

The error I get is "An unhandled exception of type 'OpenTK.Graphics.GraphicsContextException' occurred in OpenTK.dll 我得到的错误是“OpenTK.dll中发生了'OpenTK.Graphics.GraphicsContextException'类型的未处理异常

Additional information: Failed to make context 131072 current. 附加信息:无法使上下文131072成为当前。 Error: 0" 错误:0“

I've searched around and I can't find what's causing it and I'm not sure on where to start if I wanted to fix it myself. 我已经四处寻找,但是我无法找到导致它的原因,如果我想自己修复它,我不知道从哪里开始。 The code that I believe is causing it is: 我认为导致它的代码是:

    public static Texture2D GetRectangle(int width, int height, Color colour, bool fill)
    {
        Color[] _rectangleData = new Color[width * height];
        Color _fillColour;
        if (fill)
        {
            _fillColour = colour;
        }
        else
        {
            _fillColour = Color.Transparent;
        }

        //create top
        for (int x = 0; x < width; x++)
        {
            _rectangleData[x] = colour;
        }
        //create sides
        for (int y = 1; y < height - 1; y++)
        {
            _rectangleData[y * width] = colour;
            for (int x = 1; x < width - 1; x++)
            {
                _rectangleData[y * width + x] = _fillColour;
            }
            _rectangleData[y * width + (width - 1)] = colour;
        }
        //create bottom
        for (int x = 0; x < width; x++)
        {
            _rectangleData[width * (height - 1) + x] = colour;
        }
        Texture2D texture = TextureManager.newTexture2D(width, height);
        texture.SetData(_rectangleData);
        return texture;
    }

Which I call once to make a square which I can use to draw a grid. 我打电话给我做一个可以用来绘制网格的正方形。 It is static as it's part of the class I use to handle textures so I'm not passing the graphicsDevice around the whole project. 它是静态的,因为它是我用来处理纹理的类的一部分,所以我没有在整个项目中传递graphicsDevice。 Once I disable this method from being run the error doesn't appear. 一旦我禁止运行此方法,就不会出现错误。 If it is enabled, it will run for a few seconds before crashing, sometimes the window will turn black. 如果启用它,它将在崩溃前运行几秒钟,有时窗口将变黑。 I have already checked to see if it was maybe being looped by something which caused it to overload something, but it is only ever called upon once. 我已经检查过它是否可能被导致它超载的东西所环绕,但它只被召唤过一次。

I am using Empty Keys which is a GUI library for Monogame based off WPF, although this doesn't seem to be causing any issues. 我正在使用空键,这是一个基于WPF的Monogame GUI库,虽然这似乎没有引起任何问题。 I am open to changes made in order to draw a square, although I would like to know what's causing this issue so if it comes up in the future I can deal with it. 我愿意为了画一个正方形而做出改变,虽然我想知道是什么导致了这个问题,所以如果它在将来出现我可以处理它。

From your code it's hard to say, I think I need to see a lot more; 从你的代码中很难说,我想我需要看到更多; but that's an error that usually happens from two different functions accessing the same object from your content manager. 但这是一个错误,通常发生在从内容管理器访问同一对象的两个不同函数中。

For example; 例如; on a loading screen, you may have one thread loading sprites and adding them to the content manager, and the other thread drawing the loading sprite. 在加载屏幕上,您可能有一个线程加载精灵并将它们添加到内容管理器,另一个线程绘制加载精灵。 They both access the content manager and collide into that crash. 他们都访问内容管理器并碰撞到崩溃。

Consider using locks or better yet: monitor.enter / monitor.exit to protect access to any functions in your TextureManager 考虑使用锁或更好的: monitor.enter / monitor.exit来保护对TextureManager任何函数的TextureManager

monitor.enter can have a timeout time, so you can have it retry for a bit. monitor.enter可以有一个超时时间,所以你可以让它重试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM