c# - Thread queues for dummies -
I think what I think is a very common threading scenario:
- I have 100 similarities
- All jobs are independent of each other
- I want to process up to 15 jobs at a time
- Every task is completed However, the job will be started until all new jobs are completed
If you believe that every action will set an event to fire, It will be completed (I am using background worker classes), I can think of a couple ways of closing it, but I'm not sure what the "right" solution was, I was hoping that Some of you guru can point me in the right direction from there.
Solution 1: one while (continued) {threading. Sleep (1000); } Loop in my main () function. The code will be continued in the Job_Completed event handler = false when A) any work should not be queued and B) all the queued tasks have been completed I used this solution earlier And when it works fine ... it seems a bit awkward for me.
Solution 2: Use the application.Run () My Man () function will similarly call the code application in the Job_Completed event handler. No queue queue and B) all queued works are complete when the exit () is A) .
Solution 3: Use Threadpool, line up all 500-1000 requests, run them at a time (setmaxtrared) and wait for them to complete anyway Please.
In all these solutions, the basic idea is that each other job will be completed every time, unless no jobs are left. Therefore, the problem is not only waiting for the completion of existing jobs, but also wait till there is no pending job to start. If Thunderpool is the right solution, then what is the correct way to wait for Threadpool to complete all the queued items?
I think my overriding illusion here is that I did not understand how incidents happen in the fire, apparently from within my main () function, I have a Windows message loop The point-of-view does not understand the mechanic. What is the right way to resolve this problem, and why?
Even if other answers are good you want another option (you never get enough options ), Then how to think about it as an idea.
Just enter the data in a structure for each task, which is in the FIFO stack.
Create 15 threads.
Each thread will get the next job from the stack, press it.
When a thread finishes processing, get the next job, if the stack is empty, the thread dies or just sleeps, waiting.
The only complexity, which is very easy to solve, popping is happening in an important segment (read / pop synchronize).
Comments
Post a Comment