cant get rid of flicker in GDI app

fguihen

Well-known member
Joined
Nov 10, 2003
Messages
248
Location
Eire
i have overridden the onPaint method, and incorporated doubble buffering and i still have a flicker in my app. im drawing everything through my own draw method, and i am calling this through a thread. i cant get rid of the flicker, but i can reduce it if i play around with the sleep duration of the thread. any ideas anyone?
 
How are you calling the OnPaint method from the other thread? You need to be very careful causing UI updates from anything other than the applications main thread. IIRC the recomended way to repaint is to invalidate the region in question using the forms Invalidate method.
 
PlausiblyDamp said:
How are you calling the OnPaint method from the other thread? You need to be very careful causing UI updates from anything other than the applications main thread. IIRC the recomended way to repaint is to invalidate the region in question using the forms Invalidate method.

im fairly sure that im not calling the on paint method from any other thread. i only set up one thread and this is the only place the my "draw " method is being called from. i have overriden the forms OnPaint, and "OnPaintBackground" methods but still no joy.
 
Are you using the graphics object supplied by the OnPaint method? and not the dreaded CreateGraphics!
 
I am using a Graphics object from the CreateGraphics object!? why is this bad? i dont know how to get a graphics object any other way. heres how im doing it:



Code:
Graphics e = this.CreateGraphics();
this.DrawToScreen(e);

can you advise me of a better way?
 
i cant use that way im afraid. you see im using my own draw method within a thread so that i can use buttons like pause to stop my simulation. without the thread , they wont work, and to use a thread, i cant use onPaint, so i cant get a graphics objet from the OnPaint Method. any other ideas?
 
This is how I always draw, it gets rid of the flickering and I just happen to like it.

Have two graphics objects, one of them .creategraphics and one of them graphics.fromimage(buffer)
then make a bitmap named buffer, make it = a new bitmap(width,height). do all your drawing and clearing in your own draw sub. Draw using your graphics object thats from the image. then, at the end of the sub, simply have the creategraphics draw buffer to the form, never clear the form either, just have creategraphics overdraw it over and over again.
 
Hi fguihen again, I do not have any experience with threads, but could you have a variable that has global scope, that
 
Just thought id mention that often if you override the onpaint method and you are drawing the entire control, you can normally override the onpaintbackground (leaving it blank). Not doing this can sometimes cause flickering. I know you said you had overriden the onpaintbackground but you didnt say if you had blanked taken out the base.OnPaintBackground (pevent);
 
Back
Top