Must I repeat subroutines that do viertually identical things when multithreading in VB.NET

Hi all,

  I have a general question regarding multithreading in VB.Net.  I recently wrote my first program using multithreading; nothing overly complex, just something that reads in a flat text file and performs corresponding inserts/updates on a database.  The program used twenty new threads to do this.

  Due to the inability to directly pass parameters into or out of the procedure passed to the Thread constructor, I essentially repeated the same code twenty times.  Simplifying the process a bit, I created a class with twenty string variables, twenty different connections to the database, and twenty different copies of what was essentially the same code; the only difference between the copies of the code was the string names and the connections used.

  When I wanted to fill the database, I'd create a new instance of the class.  I'd then read in the twenty lines from the file and fill the corresponding string variables, then kick off the threads.  Each thread, using its own connection, would insert the lines into the database.

  To get to the point: is there any way to get around the "repeating the same subroutine twenty times" problem?  I understand that I can't directly pass parameters into the procedure used in the constructor of the Thread class, but it seems to me that there must be some way around this.  My example is pretty simple; in a complex multithreaded example, this seems like it would be a nightmare to maintain.  Any changes would have to be repeated in several different locations (the number of locations directly relating to the number of threads desired).  Any tips/advice/places to look would be greatly appreciated.  Thank you for your time.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
"I understand that I can't directly pass parameters into the procedure used in the constructor of the Thread class, but it seems to me that there must be some way around this."

If you encapsulate the Thread in a Class then you can pass in the parameters to the class and store them locally where the thread has access to them.  Now you can make multiple instances of the class with different parameters and they will all run independently of each other.

What version VB.Net are you working with?

If you show us some sample code then we can help it making it more generic...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
L_PotterAuthor Commented:
This answers my question; I just didn't realize exactly what I was doing right off the hop.  As opposed to making multiple instances of the class, I had created one class that tried to do everything (i.e. there were twenty threads created within the class, and I declared one instance of the class, as opposed to creating a class with one thread and creating twenty instances of it).  I now know for next time.  Thanks for the help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.