[dba-VB] c# - controlling the run state / status of your threaded classes

jwcolby jwcolby at colbyconsulting.com
Thu Mar 10 14:37:01 CST 2011


As I write classes that consume threads, I need to be able to start and stop these threads and the 
class that consumes the threads.  For example, I open a form and load a class instance that starts 
performing some process - exporting sets of records to csv files for example.

Once that class is loaded and processing, I need to be able to ask that class to stop exporting 
files.  However there may be a time lag between asking and the currently processing file finishing 
it's export.  That is precisely the purpose of a thread - tell it to go and do something else.

So (for example) I have a check box that I check that tells the class to start exporting.  When it 
starts it sets a flag saying that it is running and places text into the check box label saying that 
it is exporting (busy).

If I then uncheck the check box I am telling the class to stop exporting.  The class is monitoring 
the check box and will not process the next file but it can't just exit.  So it needs to set a flag 
saying that it is stopping and display "stopping" on the check box label.  When it finishes 
exporting the file, it sets a stopped flag and says displays "terminated" in the check box label.

So we have a couple of "commands" (start / stop) and a few statuses (stopped, stopping, running).

I would like to "standardize" this into a class so that any class which needs this interface does it 
in the same way.  Some controlling object can tell the object to start and stop, and the class can 
tell the controlling object that is is stopped, stopping or running.  this class does not6 need to 
do any updating of labels or anything like that, just hold the variables, properties, methods and 
code to perform this in a standard way.

Is anyone doing this?  Have existing code you would like to share?  Have existing experience trying 
to do this you would like to share?

-- 
John W. Colby
www.ColbyConsulting.com



More information about the dba-VB mailing list