Click to See Complete Forum and Search --> : Thread.Join vs Other options


shallowed
April 3rd, 2006, 10:19 AM
I'm reading up on threads and what they can do in my program... but I've ran across something that I'm debating what I should do. So here's the situation...

I'm writing up a Windows Service that looks through folders, adds info into a database about the files contained in the folders and then renames the files based on the ID I insert into the database. Then from here it will copy the renamed files into a backup folder. Since the program is running on a server with multiple processors I figure that running two seperate threads would make it go faster since the two functions are not dependant on eachother. Well, I'm running into the problem of what do I do when a serious error occurs? It would be nice to destroy the thread in the code that is doing the database transactions or backup, but how do I do that? Can it even be done?

What I'm looking at is using This.CurrentThread.Join(). But the MS specification for this method is as clear as mud for this method since this is my first time even threading (says it blocks until thread is done executing, but does that mean it will continue running my code?). There is a thread.abort() function but that will end up throwing an error all the way back up to the service, which would disrupt the service from running any further (if one thread quits it would be nice to keep the other thread alive doing its thing, then just fix the whatever is going on and then pause and resume the service).

Uhm... yeah, it's a pretty long winded explanation but basically those are my thoughts and I'm looking for advice.

wildfrog
April 3rd, 2006, 10:45 AM
I'm writing up a Windows Service that looks through folders, adds info into a database about the files contained in the folders and then renames the files based on the ID I insert into the database. Then from here it will copy the renamed files into a backup folder. Since the program is running on a server with multiple processors I figure that running two seperate threads would make it go faster since the two functions are not dependant on eachother.If these threads are heavily dependant on the same file system (the same harddrive etc,) you'll might experience the opposite; accessing the filesystem from multiple thread at the sime time may end with a performance loss.

Well, I'm running into the problem of what do I do when a serious error occurs? It would be nice to destroy the thread in the code that is doing the database transactions or backup, but how do I do that? Can it even be done?Detroying threads are not nice. You should clean up and simply return from your thread. Then optionally pass return/error information back to the main thread using a shared datastructure or something. If you wan other threads to stop (due to an error in another thread) you could set some global flag (event object etc.). Then check this flag from time to time in your thread procs.

What I'm looking at is using This.CurrentThread.Join(). But the MS specification for this method is as clear as mud for this method since this is my first time even threading (says it blocks until thread is done executing, but does that mean it will continue running my code?). You shouldn't join the current thread. You use Join to make one thread wait for another thread to finish.

is a thread.abort() function Abort is the same as destroying your thread and IMO you shouldn't use it. You should carefully clean up and return from the thread proc...

Is this a .NET project?

- petter

shallowed
April 3rd, 2006, 10:59 AM
C# 2.0 .Net is what I'm coding in.