Update Thread

P

P.J.M. Beker

Hi there,

I'm currently writing a program in which I use the FileMonitor to monitor a
folder in which I store downloaded images. I know that I can't add much
coding in the filemonitor's event in risk of losing some new entries, so
I've deceided to create an update thread. This thread is created when the
program's start and should (for various reason) run not in sync with the
Filemonitor.

The Filemonitor event creates an entry in a database table (lets call it
TEMP) in which it stores the location and name of a new image. The update
thread scans this table and if it finds any entries, is does a serie of
operations (hash calculation, check for multiple entries etc). The thread
then creates a new entry in table [QUEUE] in which it stores the image
title, location, hashvalue etc.Then it gets the next entry in table TEMP. If
there aren't any records left, it goes to sleep. When waking up it scans
table TEMP again.

Here is my problem. When terminating my program, the update thread should
stop, but only after it finishes processing the current image it's working
on. It should not create any corrupted/broken records. How can I prevent
this from happening?

// Main Program
private void FileMonitor_Created(Object sender, FileSystemEventArgs e)
{
// Creates a new record in table TEMP
CreateNewTempEntry(e.FullPath);
}

// Update Thread
private void ScanTempTable()
{
OleDbCommand myCmd = new OleDBCommand("SELECT * FROM TEMP");
myCmd.Connection = ...;
OleDbDataReader myReader = myCmd.ExecuteReader();
string myFile = "", myHash = "";
bool bFound = false;
while(myReader.Read())
{
myFile = myReader.GetString(0);
myHash = CalcHash(myFile);
bFound = Scan4MultipleImages(myHash)

-----------------------------------------------------------
// Thread isn't allow to stop here
WriteNewImageRecord(myFile, myHash, bFound);
// Thread can stop now
------------------------------------------------------------
}
// Sleep for a while
}

thnx in advance

Peter
 
J

Jon Skeet [C# MVP]

Here is my problem. When terminating my program, the update thread should
stop, but only after it finishes processing the current image it's working
on. It should not create any corrupted/broken records. How can I prevent
this from happening?

Make the thread a foreground thread, which will mean the program keeps
running while the thread is alive.

Then add a way (a flag, for instance) of telling the thread to quit
after it's finished the current record. The "sleep for a while" part
should probably be something like Monitor.Wait so you can pulse a
monitor to make it respond quickly if it's idle when you want to quit.

Jon
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi there,

I'm currently writing a program in which I use the FileMonitor to monitor a
folder in which I store downloaded images. I know that I can't add much
coding in the filemonitor's event in risk of losing some new entries, so
I've deceided to create an update thread. This thread is created when the
program's start and should (for various reason) run not in sync with the
Filemonitor.

Pardon my ignorance , but what is FileMonitor?
AFAIk the only class in the framework that do that is
FileSystemWatcher

Here is my problem. When terminating my program, the update thread should
stop, but only after it finishes processing the current image it's working
on. It should not create any corrupted/broken records. How can I prevent
this from happening?

You could use a similar mechanisn than to indicate what file to
process. Using a flag in a table (of course you have to remove the
flag before ending)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top