.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Producer/Consumer using ThreadPool.RegisterWaitForSingleObject?

Posted By:      Posted Date: September 07, 2010    Points: 0   Category :.NET Framework
Hi, I'm trying to implement an abstract daemon class for a producer/consumer scenario using ThreadPool threads. I came up with: public abstract class ThreadPoolExecuter { private AutoResetEvent _Signal; private bool _IsStopped; protected bool IsStopped { get { return _IsStopped; } } public virtual void Start() { _Signal = new AutoResetEvent(false); ThreadPool.RegisterWaitForSingleObject(_Signal, new WaitOrTimerCallback(Signalled), null, -1, true); } protected void Signal() { _Signal.Set(); } void Signalled(object notUsed, bool timedOut) { if(_IsStopped) return; try { Execute(); } catch(Exception ex) { OnError(ex); } if(!_IsStopped) ThreadPool.RegisterWaitForSingleObject(_Signal, new WaitOrTimerCallback(Signalled), null, -1, true); } protected abstract void Execute(); protected abstract void OnError(Exception ex); } The idea is that different classes could inherit from this, and use their own Execute() implementation depending on their needs. The inheriting class would Signal() whenever it needed to execute some job on the ThreadPool. I have one such inheriting class implementing a Queue for a producer/consumer scenario. My problem is that sometimes my application (a web app hosted on IIS7) seems to miss events. No error is logged. This is obviously a very hazy description of the problem, so what I want to ask is this

View Complete Post

More Related Resource Links

c#/.NET 3.5 Producer/Consumer pattern using SQL Server

Hello,I understand the producer consumer pattern but I'm finding it difficult to map it to using the Entity Data Model and SQL Server 2008. I want to implement a work queue where multiple threads will pick up the top 5 items (each) to work on and then perform somelong running task on each of those.I've googled for a an example of this but I can't seem to find one that uses the Entity Data model and C#Thanks for any tips.

Best Practices for Throttling DB Writes for Producer/Consumer Application


Hi - I'm working on a producer/consumer problem, which ultimately writes the results to a database.  We use multiple threads at each phase of the processing, including multiple threads to write the results to our database in the final phase.

We find that as we run the application more aggressively to increase throughput, we eventually start to burden the sql server, and begin to have timeouts on the database writes.

I'm trying to figure out what is the correct pattern or best practice to apply here to throttle or back-off when the sql server is burdened.  I've spent quite a bit of time looking around on the internet, but I may not be phrasing the question correctly, as this must be a common issue.  While there are a number of performance optimizations we are considering, we feel that as the sql server is a shared resource, we'll have to consider that the machine will get busy, and we'll have to back off in some fashion.


.NET Matters: Ordered Execution With ThreadPool


This month we demonstrate how you can use the ThreadPool to support ordered execution without having to build custom thread pools yourself.

Stephen Toub

MSDN Magazine February 2009

Net Matters: Round-Robin Access To The ThreadPool


Stephen Toub shows you how to add round-robin scheduling support on top of the ThreadPool for more granular processing control.

Stephen Toub

MSDN Magazine January 2009

ThreadPool with lock isn't thread safe

Hello, I'm having some problems with my code which apparently isn't thread safe at all. The problem is that it doesn't crash often, but of course it does when in production! I have twisted my mind around the problem for a month or so now, and I need some help in sorting out the problem. Well, the problem is that it isn't thread safe, and the collection (_subscribers) is modified while looping. The error says that it crashes in the method: SendRoundSettings. Here's part of the code (which I guess contains the error, as the other methods are pretty much identical): [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)] public class DuplexService : IDuplexService { private readonly object _syncRoot = new object(); private static Subscribers _subscribers = new Subscribers(); private static RoundSettings _currentRoundSettings = new RoundSettings() {CompetitionId = 1, NumberOfAthletesInRound = 0, Round = 0, SortDirection = SortDirection.ByStartNumber}; public void SendJudgeScore(CompetitionJudgeAthleteJump judgeScore) { ThreadPool.QueueUserWorkItem(state => { Subscribers clonedSubscribers = CloneSubscribers(); Console.WriteLine("Sending judgeScore to {0} client(s)", clonedSubscribers.Count(x => x.CompetitionId == judgeScore.CompetitionId));

Post the SAML Response in an HTML form to the assertion consumer service

Hi,   Can anyoner please help me in how to post the SAML Response in an HTML form to the assertion consumer service. I have generated the saml reponse and want to send the same to the re-directing url.  

How to develop a custom (media) webpart which supports webpart connections (as consumer)?

We have a list of youtube links and we want to enable user to click a list item and watch the moview in another webpart, but i didn't find a webpart which supports that (the page viewer seems suitable to display moview but does'nt support connections). So we are thinking of develop the consumenr webpart that display the moview by the parameter it gets from the list. Can you help? thanks

Develop consumer which receiving data from list view

In my page I want to display a list view. Each time a user is clicking  on an item the relevant data of the current item will be passed to a consumer web part that I will develop. What interface the list view WP inherit from? Can you give send me an example of such implementation?   Tnx Keren  

Concurrency: Throttling Concurrency in the CLR 4.0 ThreadPool

The trend toward manycore architectures and parallel apps has resulted in many changes to the CLR 4.0 ThreadPool. If you're emphasizing concurrency in new apps or adding it to old ones, or making use of ASP.NET or Parallel Extension technologies, this is for you.Erika FuentesMSDN Magazine September 2010

Seeing the SOAP request sent from a Web Service Client/Consumer

Hello, I am building a web service consumer in C# in VS2010.  Using the provided WSDL, I need to build appropriate headers and use certificates, SSL (user name & pass), & WS security.  I am getting an error from the webservice side, specifically processing the <wsse:Security> header. Is it possible for me to see the exact request I am sending to the Webservice? I'd like to see the values of the pieces of the header that get put in and such. Is it possible to have human readable serialization of what's being passed in right before the error? Thank You

ThreadPool max threads

Hi All, I'm writing an application to do some basic load tests. To start with I wanted to make sure I understood the ThreadPool. Can someone tell me why the below application seems to pause after launching 4 threads on my machine? Thanks Warrick  class Program { static void Main(string[] args) { load doit = new load(); for(int i=0; i < 10; i++) { // ThreadPool.SetMaxThreads = 20; ThreadPool.QueueUserWorkItem(new WaitCallback(doit.dowork),(object)i); } Thread.Sleep(10000); } } class load { public void dowork(object o) { Console.WriteLine("Start "+o.ToString()); Thread.Sleep(1000); Console.WriteLine("Done" + o.ToString()); } }

How to develop WCF services and host them only using WSDL and schemas provided by client(consumer)?

Hi,I am new to WCF. I have just gone through sample calculator service examples and have very hazy understanding of WCF. I have one requirement  where i need to develop WCF services. I have been provided with WSDL and SCHEMAS and has been asked to develop WCF services part.The client part is ready and they need us to write webservices so they can consume it. Can anyone please xplain how could i begin with and from where ? Also what are schemas (XSD's) used for ? Also how would I make sure that after building webservices I have hosted the webservices in such a way that other people in our network could access them ?I have read forums where you have webservices  but need to write clients which can consume it, but not sure how to approach the above requirement. Any help would be greatly appreciated. Please explain it considering I AM NOVICE/BEGINNER.  Thanks

Wait for threadpool to finish

I want to have a ThreadPool of "worker" threads that wait to be signaled to work. Once signaled it does its work then loop back to beginning and waits to be signaled again. I know how to use manual and auto resetevents to signal the thread. The only way I can see to accomplish this is to use an AutoResetEvent to toggle each thread to do work and hold for more work and use a for loop to in the main thread to call Join() on all threads. This doesn't seem elegant. Any suggestions?

FIX: After you use the ThreadPool.SetMinThreads, not working ?


Hi there

After endless hours trying to figure out this by myself I have to ask if other people has experienced the same as I.

We have a normal ASP.NET client application communicating with the web server using Ajax for some of the calls. It spawns 5 simultaneous requests to the web server, but unfortunately there is approximately 500ms between each response!

I have tried adjusting the throttling: http://msdn.microsoft.com/en-us/library/ms731379.aspx , followed the advices herein.



I have installed the hotfix http://support.microsoft.com/kb/976898 : "FIX: After you use the ThreadPool.SetMinThreads method in the Microsoft .NET Framework 3.5, threads maintained by the thread pool do not work as expected"

I have read here: http://blogs.msdn.com/b/wenlong/archive/2010/02/11/why-are-wcf-responses-slow-and-setminthreads-does-not-

can WCF be used to create consumer of the older web service technology?


I know wcf is what to use for new web services development. I have not done any web services dev at all yet, using the previous technology or wcf. Right now I'm just starting to learn about web services dev... I fully intend to use wcf for anything I do, but I was wondering if I should still learn the previous technology specifically for the case of consuming the service, for consuming existing services created using the previous technology? Can I just use wcf to create client side consumer of a web service that was created using previous technology, or would I have to use the older matching client side of the equation? 

for example, I was just reading this:

do I need to bother with the older? 

Can't activate CMIS consumer web part



I'm using the instructions at http://technet.microsoft.com/en-us/library/ff934619.aspx to try and get CMIS working.  I'm having a problem with step 9 which says:

"On the Site Actions menu of the site where the CMIS Consumer Web Part will be added, click Site Settings , choose Manage site features , and click Activate to activate the CMIS Consumer Web Part feature."

Unfortunately I don't have an option to active the Consumer Web Part feature - it doesn't appear.  The option to activate the producer feature is there ok but I don't need that.

I've followed the instructions quite carefully but I'm obviously missing something.  I've googled this but there don't seem to be many people out there using CMIS so I can't find anything relevant.

I just wondered if anyone had come across this problem before?



ThreadPool problems even after installing fix - Threads are created slowly after they are released



I have a net.tcp service hosted under IIS which is being hit concurrently by 300 clients. After doing a lot of configuration (throttling, maxconnections etc) I show that I have a thread pool problem and requests are not served quickly enough, because thread pool creates new threads every half second. There is no error. My problem is that my clients are not served quickly enough. I configured the thread pool using SetMinThreads but nothing (I also configured SetMaxThreads accordingly). Then I discovered that there is a .NET 3.5 bug and I installed the corresponding fix (http://support.microsoft.com/kb/976898). Now the behaviour I get is the following...

When the 300 clients hit the server, the server opens concurrently 300 threads (I see more than 300 threads for w3wp process in task manager) and clients are served very fast. If after this there are 300 more concurrent clients, they are being served by reusing the previous threads. Excellent!!!

However, if I let the server idle for about 10 seconds, the threads are released! w3wp threads in the task manager drop to around 25. If after this again 300 clients hit the server thread pool starts again to create threads slowly (one every half second)! Why is that?

Running Windows 7 x64, Quad Core, 8 GB RAM

Dimitris Papadimitr
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend