Re: System.Thread and multiple method calls
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Thu, 27 Dec 2007 10:23:23 -0800
On Thu, 27 Dec 2007 01:53:16 -0800, Paul Schwann <paul.schwann@xxxxxxxxx> wrote:
Hi Pete and all the others,
thanks a lot for your valuable input! Although Jon's solution is
almost perfect, it does'nt work for me. I have to be able to add and
remove any of the A, B, C, ... functions form the delegate while the
thread is *already running*. I think I will implement my own way of
doing this. Here is my first idea (sort of Java/Pseudo code): [snip]
The code you posted is not far off from the producer/consumer suggestion. However, one thing you're going to run into is the problem of modifying a List<> instance while you've got an enumerator operating on it.
You could address that by enumerating by an index that's stored not as a local variable but a member variable of the implementation class, so that it can be adjusted to account for methods removed during execution.
The other issue you'll have to solve is what to do when the client code tries to remove a method that's already running. Simplest would be to just ignore that, let the method be removed from the list without affecting the current execution of it. An alternative would be to make the methods cancellable, with removal causing a cancellation, but since that would complicate an already-complicated design, I think it'd be better if you can do without that feature.
I do think you should consider carefully whether you really must have the behavior you're asking about. There's nothing wrong with it as long as that's really the most straightforward and clean way to accomplish whatever it is you're trying to accomplish. But it's definitely an unusual request, and my experience has been that many (but not all, of course) unusual requests represent a design choice that could have been made differently, in a way that leads to a simpler implementation.
This latter point is especially true when the unusual request involves creating a complicated situation such as this one. A straight producer/consumer design wouldn't be exactly what you've asked for, but it would be much simpler architecturally. Neither are particularly difficult to implement, but conceptually having a mutable list of execution methods that can change while it's being enumerated is at the very least complicated, and at worst could lead to puzzling or subtle bugs that are very dependent on the timing of the code elsewhere. You should be sure that this is a maintenance cost that is worth whatever advantage that design has for you.
Pete
.
- Follow-Ups:
- Re: System.Thread and multiple method calls
- From: Paul Schwann
- Re: System.Thread and multiple method calls
- From: Paul Schwann
- Re: System.Thread and multiple method calls
- References:
- System.Thread and multiple method calls
- From: Paul Schwann
- Re: System.Thread and multiple method calls
- From: Scott Roberts
- Re: System.Thread and multiple method calls
- From: Ignacio Machin \( .NET/ C# MVP \)
- Re: System.Thread and multiple method calls
- From: Paul Schwann
- Re: System.Thread and multiple method calls
- From: Peter Duniho
- Re: System.Thread and multiple method calls
- From: Paul Schwann
- System.Thread and multiple method calls
- Prev by Date: Re: Problem with CGI and .NET
- Next by Date: Re: Perfomance using threading
- Previous by thread: Re: System.Thread and multiple method calls
- Next by thread: Re: System.Thread and multiple method calls
- Index(es):
Relevant Pages
|