Threading best practices and some moderately complex examples

Posted on 2007-03-29
Last Modified: 2013-11-07
I am interested in using threads and want some best practices example. I know how to start and run a simple sub but I saw some examples where delegates were used and would like to know about some more than simple applications of threading. If you have used threads in situations where it worked out great would like to know about it (i know about the basic scenarios like keeping the UI responsive, but would like some feedback from someone who has used it for something more uncommon like using beginexecutereader to get datareader in a different thread, or to populate some control periodically, etc).

Question by:shahprabal
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 13

Assisted Solution

joechina earned 150 total points
ID: 18816481
LVL 14

Author Comment

ID: 18817828
good article... meanwhile can someone share their experience and maybe some do and don't for threading...  something that I might not find in a book.
LVL 86

Accepted Solution

Mike Tomlinson earned 350 total points
ID: 18818871
Threading advice based on questions I've answered here on EE.  Some seem obvious but you'd be suprised how many questions are answered with these concepts...

(1) OBVIOUSLY...Don't update UI controls from a Thread without Delegates/Invoke...but you can "cheat" by setting the Control.CheckForIllegalCrossThreadCalls property.  You just need to know when it is safe to use the "cheat"...

(2) ALL GUI controls run in the same main UI you can use the form itself to test if an Invoke is required...such as "Me.InvokeRequired" instead of "Label1.InvokeRequired".

(3) Don't call Application.DoEvents() from a Thread.

(4) An infininte loop in a thread WILL prevent an app from exiting unless it has the Thread.IsBackground() property set to True.

(5) An infinite loop without some kind of "throttle" (or a blocking call in it) WILL ramp CPU usage to 100%.  Place a SMALL (less than 100) call to Thread.Sleep() to avoid this.

(6) Encapsulate threads in a class and pass parameters to the class.  This way you can have more than one instance of your "thread"...each with distinct parameters.  Raise events from the class to communicate back to your main form.

(7) You can create a "scheduled event" without a Timer by creating a new thread and putting it to sleep for the required amount of time.  Use (6) above to notify the main UI of the event.

(8) In general, it is a bad idea to call Thread.Abort() since this literally cuts off your threading code and doesn't allow it to clean up.  This can problems in ~some~ situations where you need to manually release memory or perform operations in some kind of "atomic" unit.  If this is the case, then create a boolean flag to toggle when a thread should exit on its own terms.  Check the flag often in your threading code.

(9) A thread "exits" and is "complete" when the code in it reaches the LAST line of code.

I'm probably forgetting something...I'll post more if I think of anything.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 14

Author Comment

ID: 18837834
Thanks Idle_Mind ... this was exactly what I wanted... pointers and dos dont from someone who has used threads in apps... is there anything that you are unfamiliar with ????????
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 18837850
"is there anything that you are unfamiliar with ????????" me...there is PLENTY in the programing world, even in VB.Net, that I have ABSOLUTELY no clue how to do!  =)
LVL 14

Author Comment

ID: 18838431
I doubt that.... Still thanks for making us feel like n00bs :)
LVL 14

Author Comment

ID: 18878461
Idle Mind, I am working on a multithreaded app and am populating a treeview control with the servers on the network in a seperate thread... even though the form loads while the control is being populated the ui is not responsive... to fix this I added Application.DoEvents in the loop that populates the control and the app behaves much better... according to ur list above calling Application.DoEvents is a no-no... can you elaborate on that...
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 18878832
You're calling DoEvents() from within the Thread?  Or after you have used Delegates/Invoke to get back on the main UI thread?

The problems I've seen with DoEvents() from a different Thread were when it was being callled repeatedly in quick succession from a Timer or infinite loop in another thread.
LVL 14

Author Comment

ID: 18878899
Basically this is what I am trying to do :

And I got the program to work both ways... i.e. using a background worker and without it...
First I tried without it... works as expected... but the UI is not responsive... when I put the DoEvents in the loop that populates the TreeView... the form UI responds better (not as good as I would have liked)...

So I looked at the background worker and implemented that solution thinking I can trigger the ProgressChanged event and have DoEvents inside the event... Also I thought since BackgroundWorker is a built in component it might work better with the UI... but triggering DoEvents through the event is not giving me the desired result...  

Also I tracked the CPU usage in both senarios... not even hitting 10%.... using Pentium D 3.00Ghz dual core cpu... any thoughts

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XML error not understood 5 27 code 9 28
.NET XML to Dictionary - XML has repeating node names 8 28
AD Computer Objects in VB .Net 2 16
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 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