Solved

Threading best practices and some moderately complex examples

Posted on 2007-03-29
9
187 Views
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).

Thanks.
0
Comment
Question by:shahprabal
  • 5
  • 3
9 Comments
 
LVL 13

Assisted Solution

by:joechina
joechina earned 150 total points
ID: 18816481
0
 
LVL 14

Author Comment

by:shahprabal
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.
0
 
LVL 85

Accepted Solution

by:
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 thread...so 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.
0
 
LVL 14

Author Comment

by:shahprabal
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 ????????
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18837850
"is there anything that you are unfamiliar with ????????"

LOL...trust me...there is PLENTY in the programing world, even in VB.Net, that I have ABSOLUTELY no clue how to do!  =)
0
 
LVL 14

Author Comment

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

Author Comment

by:shahprabal
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...
0
 
LVL 85

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.
0
 
LVL 14

Author Comment

by:shahprabal
ID: 18878899
Basically this is what I am trying to do :
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22494712.html

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
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

19 Experts available now in Live!

Get 1:1 Help Now