c# - Drawing into a shared graphics buffer out of different threads? -
i'm trying write class drawing operation directy draw onto screen. idea have constant loop, rendering graphics buffer , several functions, write graphics buffer (lines, rectangles, etc). there problem threads. can't write graphics buffer, when used in loop in different thread. need different thread keep constant performance/framerate. tried kind of create "shared" buffer in form of bitmap, performance sucks. has got idead, how solve this? current error message is: system.invalidoperationexception @ mygraphics.clear(color.black);
class drawingonscreen { intptr desktop; //a pointer desktop graphics g; //the graphics object bufferedgraphicscontext currentcontext; bufferedgraphics mybuffer; //graphics buffer thread renderthread; //the thread [dllimport("user32.dll")] static extern intptr getdc(intptr hwnd); public void drawline(vector2d point1, vector2d point2, float width, color color) //vector2d point2d { mybuffer.graphics.drawline(new pen(color, width), point1.x, point1.y, point2.x, point2.y); //trying draw line buffer } public void init() //initializing { desktop = getdc(intptr.zero); //setting drawing directly screen g = graphics.fromhdc(desktop); //setting drawing directly screen currentcontext = bufferedgraphicsmanager.current; //setting render buffer mybuffer = currentcontext.allocate(g, new rectangle(0, 0, 300, 300)); //setting render buffer renderthread = new thread(new threadstart(renderop)); //setting seperated thread rendering avoid lag etc. renderthread.start(); //starting thread } void renderop() //the render thread loop { while (true) { mybuffer.graphics.clear(color.black); //clear background mybuffer.render(); //render when every drawing operation finished } } }
thanks tries awnsering question.
Comments
Post a Comment