[dba-VB] VB.Net moving to a background worker thread

jwcolby jwcolby at colbyconsulting.com
Mon May 14 23:29:20 CDT 2007


I am using a clsProcessRawFiles that is the base unit for the transform
process I wrote.  Essentially it reads a directory to get a list of files to
process, then opens a file, reads a line, transforms the line, and writes
the line back out.  The original method I used to do this was to dim the
class and then call a method of the class to start the process of reading
the files.  The method call was in the click of a button.

clsProcessRawFiles raises three events:

evFilesToProcess (intFileCnt as integer).  This event is fired after the
method that reads the directory determines the file count, and allows the
interface to set the total units for the main file progress bar.
evCurrentFile (strFileName as string).  This event allows the interface to
display the file name being processed
evFileComplete(intCnt as integer).  This event essentially tells the main
form how many files have been processed.

As you can see, all of the file work is done in the method of the
clsProcessRawFiles and progress events fire to allow the form to display the
progress.

I also have a cancel button that feeds a Boolean property of the class to
allow the for loop processing the files to exit the loop if the cancel
property is set.

Mow... How do I move to using a worker thread?  The events of course require
the main form's thread to do the work of updating the various graphic
elements that display progress - a progress bar and a text box for the file
name.  I assume that this is illegal if the class that is processing the
files is on a worker thread?

I want a thread to run the clsProcessRawFiles.mProcessFile method so that
control is returned to the main form interface and the worker thread handles
all of the file stuff.



John W. Colby
Colby Consulting
www.ColbyConsulting.com




More information about the dba-VB mailing list