[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Help on understanding thread

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

Accepted Solution

DerZauberer earned 400 total points
ID: 36948015
LVL 53

Assisted Solution

Dhaest earned 400 total points
ID: 36948025
Understanding Threading in .NET Framework

Author Comment

ID: 36948045
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?


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 83

Assisted Solution

CodeCruiser earned 400 total points
ID: 36948059
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

ID: 36948070
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

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

LVL 16

Assisted Solution

HooKooDooKu earned 400 total points
ID: 36950611
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.

Assisted Solution

apuma earned 400 total points
ID: 36958655
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

ID: 36971734
Thank you very much!

Author Closing Comment

ID: 36971740

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month18 days, 15 hours left to enroll

834 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