I am making an indexer that needs to be processed for encryving objects. The indexer will add items to this processor. This will add 100 items for example, then adds items for 3 minutes and adds 50 other items.
public class processor {personal concurrent question & lt; Items & gt; item; Public Zero AddItem (item items) {this.items.Enqueue (item); }} Items come at random intervals, so I will dequeue these items and create a separate thread to process.
What would be the best option
, item); }
This will automatically create a row, and process the items, but I have less control, when 20 items are found, they close almost to my pointer
Use this long lasting task:
public processor () {this.task = Task.Factory.StartNew (() => DequeueItems (), Cancellation token. Some, Functioning Options. Langaran, Task Scheduler. Default); } Public DequeueItems () {While (true) {item item = zero; While (this.items.TryDequeue (item outside) {this.store.ExecuteIndex ((AbstractIndexCreationTask) item)}} Thread.Sleep (100);}} But I hate time () And thread.sleep I have got to use, because calculable will dry after some time, and it will need to be checked again if there are new items.
Short running tasks:
public processor () {} run private zeros () {this.task = Task.Factory.StartNew ((=) = DequeueItems (), cancellation token. Some, the process of operation. Firnes, Task Scheduler. Default) Public Zero AddItem (Item Item) {this.items.Add (item); if (this.task == zero) this.task.isCompleted) Run ();} Public DequeueItems () {item item = zero; While this (this.items.TryDequeue (item outside) {this.store.ExecuteIndex ((AbstractIndexCreationTask) item);}} Could this be good? But starting a thread is a " Expensive "operation, and I do not know if I can remember things because I'm checking that the loop may be in the process of ending and thus 1 item is missing but it does not sleep, and Use the dirty during the loop.
Your choice, since MSDN is TPL I recommend that you do not use threads, but maybe
(using thread solution, not TPL) ( As mentioned above) TPL is preferred when using multi-threaded code
I think the easiest solution here is to use the blocking implementation (possibly its GetConsumingEnume Using a rable () ) for a long time running work . When there is nothing to do, it will ruin a thread , but a single wasted thread is not bad. If you are unsure that threads , you can go with something like your # 3 but you have to be very careful about making this thread-safe. For example, if your code is not running the job and AddItem () is said at the same time with two threads, then you have two tasks s, which is almost certainly false. Another option, if you are on .NET 4.5, then use the ActionBlock from TPL dataflows. With it, you are not wasting any thread and you do not have to write yourself a hard thread-protected code.
No comments:
Post a Comment