threads

  • Thread starter Thread starter Jeroen Ceuppens
  • Start date Start date
J

Jeroen Ceuppens

I have a problem about how creating a thread:

I need a thread for drawing, because i also need to change options in some
textboxes

The drawing thread draws bmp's from a source (digital camera)

Is is possible to add the OnPaint in a thread? or how can i make it that I
can do the drawing in a thread?

protected override void OnPaint(PaintEventArgs e)

{

if (draw)

{

for (;;)

e.Graphics.DrawImage(bmp,100,100); //the bmp file changes
everytime

}

base.OnPaint(e);

}
 
You can't draw on a separate thread. You code never yields at all, why not
just do it in OnPaint and use a timer to refresh periodically?
 
Ok, maybe i need a timer to refresh, but i still need a thread, how can I
otherwise do something else?

Thx
JC
 
You cannot draw from another thread. Period. I don't see why you need
another thread in this case, though. The reason it was causing a lock as
written is that you're in a tight loop that never yields, which is just bad
programming. In some situations a Sleep() in the loop would fix it, but
because this is the primary thread, a Sleep call will still make the app
seem locked. The "fix" is to just put the drawing in the standard OnPaint,
then use a timer to cause an invalidation or refresh or whatever, which will
in turn cause OnPaint to occur with the same frequency.

You can adjust your timer based on how often you actually need painting. The
human eye sees at about 28 fps, which is roughly a period of 36ms, so any
faster is a waste of processor cycles, and if you're not trying to do
animation or video, 15-20fps (50-66 ms) is usually plenty fast enough Your
loop is running way faster than this, which explains the adverse behavior
youre seeing.
 
Back
Top