views:

276

answers:

6

I have a C# winform application. it has many forms with different functionalities. These forms wrap to a WCF service. for example

form1 calls serviceMethod1 continuously and updates the results

form2 calls serviceMethod2 continuously and updates the results

The calls are made in a different thread per each form, but this is ending up with too many threads as we have many forms. Is this bad and why? and is there a way to avoid this given my scenario?

Regards

+4  A: 

How many threads are you talking about? If you have a lot of threads, you'll lose a bit of performance due to context switching - but in practice I wouldn't expect this to become a significant problem until you have an awful lot of them.

One alternative would be to use a Timer though (it sounds like a System.Timers.Timer or System.Threading.Timer would be most appropriate) - schedule each service call to be made on a regular basis, and the timer will use the threadpool to fire the calls. I suspect that although you say you're calling the services "continuously" you actually mean you're doing it regularly - which is exactly the kind of situation a timer is good for.

Jon Skeet
Well I am talking about 6 threads/ MDI parent. The user has the ability to have up to 10 similar MDI parents (called views), but it's not likely he might open more than 2 of them. However, of course I cant rely on that.I think I will go for the threadpool solution, and decrease the upperbound of the number of opened views
Mustafa A. Jabbar
@Jon - out of interest, how many is an *awful lot*?
Winston Smith
@Winston: Good question - obviously it'll get worse the more you have, and also depend on what the threads are doing. If they're idle, it shouldn't be *too* bad... but you'd really need to test it under appropriate conditions to see how much performance difference there is for the particular application. There are too many variables to make a specific claim here :)
Jon Skeet
A: 

While the threads spend most of their time waiting for server response - even hundreds of threads are unlikely to degrade performance (CPU-wise). Otherwise, use thread pool and queue "request and update form once" tasks when previous update completes.

More important problem might be loading service with too many simultaneous requests.

ima
+1  A: 

To answer the question frankly: It depends entirely on the OS and app design, but this question may indicate a shortcoming in the program's design.

Detail: You want to learn the allocation requirements of a thread on your target architecture/OS, as well as keep your threads relatively busy/avoid polling, and to configure priorities correctly if you really do have a lot of threads. 'Many' threads may be 8 (or fewer, if busy), or 100+ if they have relatively little work to do, it ultimately depends on your needs and design.

As tests for some tests/objects/operations, I have used more than 100, and occasionally more than 1000 working threads. No explosions happened, though I have never had a true need for those operations to be that parallel in a shipping app (unless the aforementioned programs are being used in very unusual circumstances), and it made more sense to put the actual implementation into some centralized task manager. If you have time-critical/real time applications, then these tasks may be best on another thread. If they are short lived, consider a thread pool.. well, there are many ways to attack many problem classes...

Justin
A: 

As a general rule, you won't gain anything by having more threads than you have CPU cores. There are exceptions to the general rule, but I doubt they apply to your case.

From the OS' point of view, threads are no longer the lightweight things they used to be, but are almost as costly as full processes. Implementing thread synchronization correctly is not a simple task, debugging multi-threaded applications is a lot harder than a single threaded one.

Anton
That's a wrong rule. Modern CPUs provide a significant degree of parallelism within core, and one thread often is unable to fully utilize it. For example, when one thread gets a cache miss, second one will get free time to execute on the same core.
ima
Moreover, writing synchronous code which blocks on IO is generally simpler than writing asynchronous code. It's easier to have a number of synchronous web service calls (more than you have cores) than to go round the houses to do it all asynchronously.
Jon Skeet
A: 

With green threads, it is not an issue. Green threads being sort of a virtual thread, which is what you will generally get with Java and C#.

The benefit of threads in many apps is not to crunch more numbers but to allow lots of things to go on at once with good responsiveness, so having a lot of threads can be very useful for some things and will not always have any real cost.

Charles Eli Cheese
+1  A: 

You can use WCF asynchronious proxy

In Visual Studio, when you add Web Reference you can check "Generate Asynchronous operations" to generate an asynchronious proxy.

MuiBienCarlota