Threads

Torro

Member
Aug 14, 2002
163
0
0
Quick question
I'm introducing myself to threads (in C# but I guess principles are principles)
Is there a way to tell whether there any user spawned threads running?

App:
Doing database insertion.
so you can insert records from a file. (call them dacs)
each file can have thousands of dacs to insert and the ui allows you to specify many files at once
i didn't want to have the ui freeze up on the user while the records are being inserted (it can take a while) so i had each file spawn a new thread
the thread will open the file, get the records and insert them

however i didn't want the user to be able todo something silly during this process (like click insert on the same files again) so i disabled the buttons while the insertion is taking place

i need a way to tell when all the insertions are done so i can reactivate the buttons

currently the way i have this implemented is every time i spawn a new thread, i name that thread the same as the filename (filenames are unique)
i then add that filename to an arraylist
when all the threads are created, i spawn a new thread (mon) that monitors the size of the arraylist
the last thing that any thread does is remove the filename from the arraylist
when the arraylist is empty (all threads have stopped running) , mon reactivates the buttons and shows the status of the insertions etc

is there an easier way to tell when the threads have stopped running? (well i guess technically i don't have to add/remove the filename, but add/remove from position 0 should be sufficient but you get my point)
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
That's basically how I do it. You could perhaps simplify it. Launch one thread which then launches the multiple other threads and keeps an eye on them. When done, it does whatever to notify the main thread that it is done. That way you can just call one thread, pass it a list of files, and let it take care of the rest.
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
Well, once you figure out inter-thread communication, why not just restrict yourself to two threads... one for the GUI and one that handles all of the db stuff?
 

thornc

Golden Member
Nov 29, 2000
1,011
0
0
And don't forget about thread synchronization... this is a major problem also!
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: Torro
but how would it keep an 'eye' on the other threads?

when all the threads are created, i spawn a new thread (mon) that monitors the size of the arraylist
^^^^ however you did that :) (and probably many other ways)

Just pass references of something to the threads you spawn, then they can fiddle with that "thing", and you can check its value to see what's happening (this is all so vague, because it really just depends on the situation and language and whatnot)