Click to See Complete Forum and Search --> : workerthread drawing program


Chipmunk Baby
June 16th, 2008, 09:37 PM
I have a progream that draws on form using a data file

I call the bacground worker thread in in Form_Paint

this.bgworkerthread.RunAsync...() //i forgot the predefined funtion name
while(isbusy)
{application.doevents()}

and in the doWork of bgworkerthread I try drawing

Also , I have zooming function for the graph when the datanumber is large
which again calls Refresh() for form,

This way, I don't know if it is correct but the workerthread work fine (I can close the form white reading or drawing large file).
Only the the zoom fails because "the the workerthread can not be used with multiple tasks at the same time"
I debug and see it's the Refresh that causes the exception.

Is my way of implemeting the workerthread incorrect ?

Thanks

cjard
June 17th, 2008, 03:45 AM
no no no

What is the point of starting another thread to do the background work, if youre then just going to go into a tight loop on DoEvents with the UI thread

The whole idea of using a backgroundworker is that you can set it going and it works away, and you let the (usually UI but not necessarily) thread that started it fall idle. In this case, it's a UI thread and you NEED to let it fall idle so it can keep the UI responsive, handling events etc.. Tight looping on DoEvents is NOT the way to do it (and i'd love to shoot people who do ;) ). DoEvents should have been removed a loong time ago, to encourage people to code better

One other thing.. dont do any drawing from the worker code; BGW manages the invokes necessary to have the UI thread do the painting, jsut ReportProgress whenever you want some drawing done and pass an object that represents the drawing to do