Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 多线程处理集合中具有特定顺序的项目C_C#_.net_Multithreading - Fatal编程技术网

C# 多线程处理集合中具有特定顺序的项目C

C# 多线程处理集合中具有特定顺序的项目C,c#,.net,multithreading,C#,.net,Multithreading,我有一个关于多线程的问题。也许这个问题很容易解决,但我不知道哪条路最好 我有一些元素的集合,例如列表 让我们假设此集合包含以下元素: SportEventSettings_1; SportEventSettings_2; SportEventSettings_3; SportEventSettings_4; SportEventSettings_5 我使用.net任务并行库中的Paralle.ForEach方法在不同线程中处理此集合并发送给客户。但是,在这种情况下,我们不能保证在我方处理后,这些

我有一个关于多线程的问题。也许这个问题很容易解决,但我不知道哪条路最好

我有一些元素的集合,例如列表

让我们假设此集合包含以下元素: SportEventSettings_1; SportEventSettings_2; SportEventSettings_3; SportEventSettings_4; SportEventSettings_5

我使用.net任务并行库中的Paralle.ForEach方法在不同线程中处理此集合并发送给客户。但是,在这种情况下,我们不能保证在我方处理后,这些来自合同的要素将以相同的订单发送给客户。我如何决定这一点,并将根据收集中的订单发送此项目

附言不重要!!!在我方加工定货。但重要的发送列表项的顺序与at list相同

谢谢

使用。。而不是这允许并行处理项,并且仍然按照输入序列中显示的相同顺序枚举已处理项

var inputItems = new List<int>(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });//List<int> is just example, input sequence can be any IEnumerable<T>

var processedItems = inputItems
    .AsParallel()//Allow parallel processing of items
    .AsOrdered()//Force items in output enumeration to be in the same order as in input
    .WithMergeOptions(ParallelMergeOptions.NotBuffered)//Allows enumeration of processed items as soon as possible (before all items are processed) at the cost of slightly lower performace
    .Select(item =>
        {
            //Do some processing of item
            Console.WriteLine("Processing item " + item);

            return item;//return either input item itself, or processed item (e.g. item.ToString())
        });

//You can use processed enumeration just like any other enumeration (send it to the customer, enumerate it yourself using foreach, etc.), items will be in the same order as in input enumeration.
foreach (var processedItem in processedItems)
{
    //Do whatever you want with processed item
    Console.WriteLine("Enumerating item " + processedItem);
}

如果您非常关心顺序,您可以使用ConcurrentQueue之类的工具,但是这是FIFO逻辑,因此在删除项目时需要小心

如果你正在寻找一个完全灵活的选择,还有BlockingCollection,在这种情况下非常方便。是一篇好文章,是MS文档的原件

在下面,您可以看到尝试从列表中获取项目时出现的情况:

BlockingCollection<MyClass> bCollection = new BlockingCollection<MyClass>(boundedCapacity: 2);
bCollection.Add(new MyClass{ Field1 = "Test" });
bCollection.Add(new MyClass{ Field1 = "Test2" };

var item = bCollection.Take();
item = bCollection.Take();

if (bCollection.TryTake(out item, TimeSpan.FromSeconds(1)))
{
    Console.WriteLine(item);
}
else
{
    Console.WriteLine("No item removed");
}

在这种情况下,您应该异步处理消息,但应按您需要的顺序同步和准确地发送消息。消息的可能重复