Why does iterating over GetConsumingEnumerable() not fully empty the underlying blocking collection

前端 未结 4 1939
天涯浪人
天涯浪人 2021-02-04 04:16

I have a quantifiable & repeatable problem using the Task Parallel Library, BlockingCollection, ConcurrentQueue & GetCo

4条回答
  •  北恋
    北恋 (楼主)
    2021-02-04 04:35

    I feel like I should note just for clarity that in instances where you are able to call the BlockingCollection's .CompleteAdding() method prior to executing the Parallel.foreach, the issue you describe above will not be a problem. I have used these two objects together many times with great results.

    In addition, you can always re-set your BlockingCollection after calling CompleteAdding() to add more items when needed (_entries = new BlockingCollection();)

    Changing the click event code above as follows would solve your problem with the missing entry and make it work as expected, if you click the start and stop buttons multiple times:

    private void button2_Click(object sender, EventArgs e)
    { //STOP BUTTON
        timer1.Stop();
        timer1.Enabled = false;
    >>>>_entries.CompleteAdding();
    >>>>_entries = new BlockingCollection();
    }
    

提交回复
热议问题