Help on understanding thread

Posted on 2011-10-11
Last Modified: 2012-05-12
What are threads and what are the Pros and Cons
Question by:rayluvs
    LVL 5

    Accepted Solution

    LVL 53

    Assisted Solution

    Understanding Threading in .NET Framework

    Author Comment

    To be more specific, we came upon the term "thread" in visual basic .Net when an application we are working in became super slow when processing.   The reason is that the apps has to run a series a routine for different companies.  The way is set up now, the routines are run one by one, company by company; hence super slow.

    While googling we found that instead calling the subroutine directly, we call it via "thread".  The process or routine is executed and the controls return and can be used to run the next company.  This in turns make the subroutine run simultaneously.

    We are not knowledgeable yet in using thread and since it's a lot code, we wanted an experience opinion on EE behalf.

    What you guys think of of thread?
    When should we really consider using this method?
    Any ram or memory considerations in the PC?

    LVL 83

    Assisted Solution

    It really depends on your code and we would not be able to give proper advice without seeing the code and understanding what it does. Generally speaking, you can run any method on a separate thread as long as there is no UI code in that method.

    It appears that in your case, a queue of tasks and a pool of threads would do the trick. But you would need to make sure the threads do not try to access same resource simultaneously and if they do then you would use locking.

    Author Comment

    Thanx for the links, but can you gives us in your own experience or words?  We mean like what have been your experience when you first came upon thread.  Also how did you work around when incorporating thread to your existing code.

    Greatly appreciated

    Author Comment

    Understood.  We don't expect direct advice as to our own developing environment, just some experience guidance on the matter.

    LVL 16

    Assisted Solution

    Think of a thread like a program.  Just like you can run multiple applications simultaneously on a single computer, you can run multiple threads simultaneously.  

    Of course actually, the threads/programs do not run "simultaneously", they instead execute in time slices from the CPU.  Basically, the CPU executes one program/thread for a few moments, then just to the next program/thread for a few moments, and back again.  (With some modern multi-core CPUs, some of the logic will actually execute simultaneously as one core might be working on one thread/program while the other core works on another thread/program.)

    However, where programs run in their own memory space (so that one program can not access data from another program), multiple threads within one program do run in the same memory space.  As such, the logic the thread is running must be made "thread safe".  As an example, if two threads could access the string variable A$, thread A might attempt to execute the command "A$="ABCDEFG", and thread B might attempt to execute the command "A$="123456".  Thread A might get as far as writing "ABC" before thread B comes in and starts writing "1234", then thread A might write "DE", then thread B writes "56", etc.  By the time the two threads finish writing to A$, you wind up with something like "123D5FG".  (In reality the results would be worst, because creating a string involves many steps including allocation of memory, so at the two threads attempt to write to the same string, nasty things might really happen).

    So depending upon your processor, going multithreaded might or might not improve the overall performance (because in a single CPU core machine, the overall process will take longer as all the same work has to be done, but the CPU has to add the overhead of swapping between threads).  But the single biggest issue you will face is what CodeCruiser points out... the threads accessing resources simultaneously.
    LVL 1

    Assisted Solution

    I have found that if your code spends allot of time waiting for multiple responses from outside sources (web services, Databases etc..) then adding threading can significantly increase performance

    I.E. if your application is doing something like this  (and thee operations don't rely on the previous operation completing)
    getDBResult (2 Seconds)
    getanotherDBResult (3 seconds)
    sendanemail (0.5 Seconds)
    writeaDBRecord (0.5 Seconds)

    it would take 6 seconds to complete.  (2+3+0.5+0.5)

    whereas this
    Threading.ThreadPool.QueueUserWorkItem(AddressOf getDBResult)
    Threading.ThreadPool.QueueUserWorkItem(AddressOf getanotherDBResult )
    Threading.ThreadPool.QueueUserWorkItem(AddressOf sendanemai)
    Threading.ThreadPool.QueueUserWorkItem(AddressOf writeaDBRecord)
    Wait for all threads to complete  

    in theory this would only take 3 seconds (the longest individual operation)

    but there is allot more management to perform for thread safety and control as pointed out in the other posts.

    Author Comment

    Thank you very much!

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now