Ich habe ein ConcurrentDictionary der Attribute für Produkte. Diese Attribute haben die Produkt-ID und Werte für die Namen des Attributs und alle Optionen, die das Attribut hat. Ich habe dieses ConcurrentDictionary, weil ich Threads habe, die erstellt werden, um jedes Attribut in dem Wörterbuch nach Attributname zu behandeln.Multithread, Linq zu Sql, ConcurrentDictonary Fails Entfernen
if (knownAttribute.AttributeType.Value.Equals("Product Specification"))
{
Console.WriteLine("Started a thread for: " + knownAttribute.AttributeTypeId + ", " + knownAttribute.Value);
while (true)
{
/* if (AS400SpecificationAttributes.IsEmpty && knownSpecificationBag.IsEmpty && gatherRowsTasks.All(x => x.IsCompleted))
break;*/
AS400SpecificationAttribute AS400SpecificationAttributeWork = null;
AS400SpecificationAttributeWork = knownSpecificationBag.Keys.FirstOrDefault(x => x.AttributeName == knownAttribute.Value);
if (AS400SpecificationAttributeWork != null)
{
var product = ctx.Products.FirstOrDefault(x => x.ProductNumber == AS400SpecificationAttributeWork.ProductNumber);
if (product == null)
continue;
var productAttribute = new ProductAttribute();
productAttribute.Attribute = knownAttribute;
if (AS400SpecificationAttributeWork.AttributeValue != null)
{
var knownAttributeOption = ctx.AttributeOptions.FirstOrDefault(x => x.Attribute.Equals(knownAttribute) && x.Value.Equals(AS400SpecificationAttributeWork.AttributeValue));
if (knownAttributeOption == null)
{
knownAttributeOption = new AttributeOption();
knownAttributeOption.Value = AS400SpecificationAttributeWork.AttributeValue;
knownAttributeOption.Attribute = knownAttribute;
ctx.AttributeOptions.InsertOnSubmit(knownAttributeOption);
ctx.SubmitChanges();
}
productAttribute.AttributeOption = knownAttributeOption;
productAttribute.AttributeOptionId = knownAttributeOption.Id;
}
product.ProductAttributes.Add(productAttribute);
ctx.SubmitChanges();
string tmpstr = null;
if (!knownSpecificationBag.TryRemove(AS400SpecificationAttributeWork, out tmpstr))
Thread.Sleep(50);
}
else
{
if (tryCounter < 5)
{
tryCounter++;
Thread.Sleep(1000);
Console.WriteLine("Thread waiting for work: Product Specification:" + knownAttribute.Value);
continue;
}
else
{
int outVal;
threadTracker.TryRemove("Product Specification:" + knownAttribute.Value, out outVal);
Console.WriteLine("Closing Thread: Product Specification:" + knownAttribute.Value);
break;
}
}
Thread.Sleep(50);
}
Es scheint, als ob das folgende Attribute-Element nicht entfernt werden soll.
Ich verstehe nicht warum. Wenn ich es eine Weile (! Dic.tryRemove (ele)) lege es wird für immer fest und nie von dort bewegen.
Es kann ein Fehler irgendwo innerhalb des Threads sein, aber ich habe keine Ahnung warum.
Ich kann nicht den gesamten Kontext sehen - aber, was passieren würde, wenn das Element nicht im Wörterbuch ist oder wenn zwei Threads beide den gleichen Code ausgeführt haben, also hat einer von ihnen ihn entfernt und für den anderen Thread war er nicht da? –