锁在c#web应用程序中无法正常工作

时间:2017-08-04 09:47:46

标签: c# asp.net multithreading web-applications

我创建了一个函数,我创建了一个锁,所以在第一次调用完成之前没有其他人访问该代码。在这个锁下我运行并行线程并进入该线程我调用的函数也有自己的锁,但有些时候第一个锁在某个时间后无法运行并且应用程序崩溃。

static readonly object _syncCoreLock = new object();

internal void ExecuteCatalogTablePopulationThreads()
    {
        lock (_syncCoreLock)
        {
            currentContext = System.Web.HttpContext.Current;
            System.Collections.ObjectModel.Collection<System.Action> actions = this.CheckCacheAndExecuteActionIfCacheExpired();

            if (actions.Count > 0)
            {
                System.Action[] methodsToExecute = new System.Action[actions.Count];
                actions.CopyTo(methodsToExecute, 0);
                homeCategoryId = DefaultParametersForCatalog.HomeCategoryId.ToString();
                System.Threading.Tasks.Parallel.Invoke(new System.Threading.Tasks.ParallelOptions { MaxDegreeOfParallelism = actions.Count }, methodsToExecute);
            }

            this.ExecuteMetaTablesQueryInMultipleThreads<AttributeStringMetaDictionary>();

            if (this.isExecuteProductMetaAttributeQueryThread || this.isExecuteSkuMetaAttributeQueryThread)
            {
                this.SearchDataDictionary = new System.Collections.Generic.Dictionary<int, SearchKeywordDictionary>();
                System.Threading.Tasks.Parallel.Invoke(new System.Threading.Tasks.ParallelOptions { MaxDegreeOfParallelism = 3 },
                    () => this.ExecuteQualifierTableInThread<ProductQualifier>(),
                    () => this.ExecuteCrossSellingTableInThread<CrossSellingProduct>(),
                    () => this.ExecuteProductSkuMapping());
            }

            if (this.isExecuteCategoryMetaAttributeQueryThread || this.isExecuteProductMetaAttributeQueryThread)
                System.Threading.Tasks.Parallel.Invoke(new System.Threading.Tasks.ParallelOptions { MaxDegreeOfParallelism = 2 },
                                                        () => this.ExecuteProductCategoryAndQualifierMapping(),
                                                        () => this.ExecuteCategoryMetaFieldMapping());

            if (this.isExecuteProductMetaAttributeQueryThread || this.isExecuteSkuMetaAttributeQueryThread)
            {
                this.ExecuteProductMetaFieldMapping();
                this.ExecuteSkuMetaFieldMapping();

                this.ExecuteCrossSellingProductMapping();

                this.SearchKeyWordDataDictionary = new System.Collections.Generic.HashSet<SearchKeywordDictionary>(this.SearchDataDictionary.Values);
                Cache.Insert(searchDataDictionaryCacheKey, this.SearchKeyWordDataDictionary);
            }

            if (this.isExecuteDiscountMappingThread)
                ExecuteProductDiscountMapping(); 
 }
}

有时会抛出以下错误:

  

System.AggregateException:发生了一个或多个错误。 ---&GT; System.OutOfMemoryException:抛出了类型'System.OutOfMemoryException'的异常。      在System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache 2.Resize() at System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache 2.Find(K key,T&amp; value,Boolean add)      在System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache 2.InsertLookup(Object instance) at System.Data.Linq.IdentityManager.StandardIdentityManager.InsertLookup(MetaType type, Object instance) at System.Data.Linq.CommonDataServices.InsertLookupCachedObject(MetaType type, Object instance) at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReaderBase 1.InsertLookup(Int32 iMetaType,Object instance)      在Read_UpSellingProduct(ObjectMaterializer 1 ) at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader 2.MoveNext()

任何人都可以告诉我这是什么问题吗?

谢谢, 沙

0 个答案:

没有答案