|
|
Mysteries of the NET Framework: Question 4
-
09-23-2008, 3:40 AM |
|
|
Mysteries of the NET Framework: Question 4
|
-
09-23-2008, 9:26 AM |
-
kpkeller
-
-
-
Joined on 10-10-2007
-
-
-
-
|
Re: Mysteries of the NET Framework: Question 4
Although it's extremely rare when I've had to use more than a single threaded app, the last one I built used two threads. One for the UI and one that handled a long-running (about 3 minutes) process that the user kicked off from within their UI.
Once it was complete, it made a callback to a UI method to alert the user that the process had completed.
|
|
-
09-23-2008, 4:46 PM |
-
Damon
-
-
-
Joined on 06-26-2006
-
Dallas, TX
-
-
-
|
Re: Mysteries of the NET Framework: Question 4
Simple: when one thread isn't fast enough for you =]
Damon Armstrong, Technology Consultant [ Blog] [ Articles]
|
|
-
-
09-29-2008, 10:17 AM |
-
David Connell
-
-
-
Joined on 11-14-2005
-
-
-
-
|
Re: Mysteries of the NET Framework: Question 4
Rob, have you used multiple threads to do UI?
If so why have you done this and what benefits does it have?
Also what are the pitfals?
David
|
|
-
-
09-30-2008, 8:35 AM |
-
Steve S
-
-
-
Joined on 09-07-2007
-
-
-
-
|
Re: Mysteries of the NET Framework: Question 4
In ASP.NET, you can choose to handle requests "asynchronously", which means request processing is split in two. The first half runs on one thread, from which you launch some expensive I/O bound operation such as a database query, then cede control and return the I/O operation's IAsyncResult back to the ASP.NET framework. Later, when the IAsyncResult signals completion, the framework takes another thread from its threadpool and calls your page to do its second half of processing and render an HTML page.
The whole idea of this is to reduce the number of threads that are sitting around waiting for I/O to complete.
|
|
-
09-30-2008, 9:52 AM |
-
David Connell
-
-
-
Joined on 11-14-2005
-
-
-
-
|
Re: Mysteries of the NET Framework: Question 4
Rob,
That's very interesting about checking for NULL, Disposing and IsDisposed
have you ever been caught out by the order of these calls?
Also I could see how you can reduce the window of threads calling controls that have been disposed, but can you elimate the problem all together?
Isn't there some theoretical window?
Do you like using async. calls between the threads or sync ? Does this impact on performance?
Regards
David
|
|
-
09-30-2008, 10:13 AM |
-
RobertChipperfield
-
-
-
Joined on 11-21-2006
-
Cambridge, UK
-
-
-
|
Re: Mysteries of the NET Framework: Question 4
I'll answer the sync or async bit first; it's a bit easier :-).
I normally go for a synchronous invoke when going back on to the GUI thread from a worker - it's normally the case that the worker thread is finished once the final GUI update has happened, so it's not too much of a problem to block that. And of course, the GUI update should be fairly quick anyway.
Aync invokes can be really useful on delegates using the "BeginInvoke" method, but make sure you call "EndInvoke" exactly once for every BeginInvoke, or you'll end up with either an exception (calling it twice) or a possible memory leak, according to Microsoft, if you don't call it at all.
I normally do "Diposing || IsDisposed || !HandleCreated" - the theory being, if something is in the process of being disposed, it's going to go through the "Disposing" step into "IsDisposed", then stay there.
I'm not sure that you can close the window of vulnerability completely by just using those calls, but it's normally good enough in practice.
I guess if you wanted to be absolutely bombproof, you could do something like the following: - Have a member variable (volatile, of course) that indicates an invoke is about to happen - Before checking IsDisposed etc, set it to true - Wherever the form is disposed from, check that that member isn't set, and if it is, "deal with it somehow", where "somehow" is a little bit undefined here :-) - On the other thread, having entered the Invoke, unset the marker.
This needs a bit more thought really because you might have two threads both setting the marker, so you'd need to make sure one doesn't unset it when the other is still going, but you get the idea...
Robert Chipperfield Developer Red Gate Software Ltd
|
|
-
10-01-2008, 12:44 AM |
-
jezemine
-
-
-
Joined on 02-05-2007
-
Seattle
-
-
-
|
Re: Mysteries of the NET Framework: Question 4
In my job I do a lot of math - multiple threads are essential in large calculations that can be parallelized, particularly when you are running on a multiproc system.
elsasoft.org
|
|
-
10-01-2008, 6:20 AM |
-
BrentBurke
-
-
-
Joined on 10-08-2007
-
-
-
-
|
Re: Mysteries of the NET Framework: Question 4
When work can be done independently from another and the performance overhead of context switching is lower than the expected profit of doing it parallel
|
|
-
10-01-2008, 7:29 PM |
-
Lewis Moten
-
-
-
Joined on 10-06-2006
-
-
-
-
|
Re: Mysteries of the NET Framework: Question 4
Multithreading is preferable when you have a large amount of work to be done in a background process that does not depend on being completed sequentially.
|
|
-
10-05-2008, 7:04 PM |
-
mrhassell
-
-
-
Joined on 10-06-2008
-
Melbourne, Australia
-
-
-
|
Re: Mysteries of the NET Framework: Question 4
Shared resources are the primary way of introducing threads in a relative fashion. As mentioned also services (which typically are dependant on multiple levels of shared resources) can benefit greatly from multi-threading.
A simple example is the "Cache" method where an item has been added to a data collection and is not yet contained within the global cache collection.
Rebuilding a new cache to include the added object/s, using a tear down method would introduce local Mutex threads, which would require a release of the thread Mutex.
|
|
-
10-15-2008, 1:40 PM |
-
jberke
-
-
-
Joined on 10-15-2008
-
-
-
-
|
Re: Mysteries of the NET Framework: Question 4
I recommend that you try to refrain from explicitly creating threads, and instead rely upon the BeginInvoke, EndInvoke pattern, or utilize the thread pool; however, there are some cases where the BeginInvoke pattern might not be available, or the thread pool might be the best option.
For example if your building a component you might want to shy away from the threadpool not knowing what process your going to be hosted in or how it will use the thread pool.
Put that aside the types of activities I'd look at pushing to other threads are:
- Any call that requires going over the network such as a web service, database etc.
- Intensive disk activity
- CPU intensive algorithims which could benefit from leveraging multi-core machines (I'd give the Parallels library from MS a shot before trying to thread an algorithim myself).
-Josh
|
|
-
Page 1 of 2 (20 items)
1
|
|