内部に Linq クエリを含む foreach ループがあります。
foreach を Parallel.ForEach に変更するまでは、すべてうまくいきます。
// get the task info ---------
Log("Populate task, guf code lists ...........................");
List<SF_CO_ITEM> tasks = (from coi in ctx.SF_CO_ITEM
where coi.CO == co.ID
select coi).ToList();
// foreach (SF_CO_ITEM t in tasks)
// {
Parallel.ForEach(tasks, t =>
{
Log("Executing on t: " + t.ID);
// exception on next line:
List<SF_CO_LINE_ITEM> gufs = (from coli in ctx.SF_CO_LINE_ITEM
where coli.CO_ITEM == t.ID
select coli).ToList();
そして、私が得る例外は次のとおりです。
System.AccessViolationException は処理されませんでした Message=保護されたメモリを読み書きしようとしました。これは多くの場合、他のメモリが破損していることを示しています。Source=Oracle.DataAccess StackTrace: at Oracle.DataAccess.Client.OpsCon.Open(IntPtr& opsConCtx, IntPtr& opsErrCtx, OpoConValCtx* pOpoConValCtx, OpoConRefCtx& pOpoConRefCtx) at Oracle.DataAccess.Client.ConnectionDispenser.Open(OpoConCtx opoConCtx) at Oracle.DataAccess. Client.OracleConnection.Open() で System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf (ブール値の openCondition、DbConnection storeConnectionToOpen、DbConnection originalConnection、String exceptionCode、String attemptsOperation、Boolean& closeStoreConnectionOnFailure) で System.Data.EntityClient.EntityConnection.Open() で System.Data.EntityClient.EntityConnection.Open() 。データ。
1.GetResults(Nullable
1 forMergeOption) System.Data.Objects.ObjectQuery で1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() at System.Collections.Generic.List
1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable
1 source) ChangeOrder.Program.<>c_ DisplayClass19.b _16(SF_CHANGE_ORDER_ITEM t) で C:\VS_apps\PMConsole\PMC Tools\ChangeOrderExecution\Program .cs: System.Threading.Tasks.Parallel.<>c_ DisplayClass2d の 220 行目2.<ForEachWorker>b__23(Int32 i) at System.Threading.Tasks.Parallel.<>c__DisplayClassf
System.Threading.Tasks.Task.InnerInvoke() の System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask) の 1.b _c( ) System.Threading.Tasks.Task.<>c_ DisplayClass7.bでSystem.Threading.Tasks.Task.ExecuteSelfReplicating(タスク ルート) の _6(オブジェクト) System.Threading.Tasks.Task.Task.ExecutionContextCallback(オブジェクト obj) の System.Threading.ExecutionContext .Run(ExecutionContext executionContext、ContextCallback コールバック、オブジェクト状態、Boolean ignoreSyncCtx) System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot) at System.Threading.Tasks.Task.ExecuteEntry(Boolean bPreventDoubleExecution) at System.Threading.Tasks. ThreadPoolTaskScheduler.TryExecuteTaskInline (タスク タスク、ブール値の taskWasPreviouslyQueued) の System.Threading.Tasks.TaskScheduler.TryRunInline (タスク タスク、ブール値の taskWasPreviouslyQueued、オブジェクト threadStatics) の System.Threading.Tasks.Task.System.Threading.Tasks.Task.RunSynchronously(TaskScheduler スケジューラ) の InternalRunSynchronously(TaskScheduler スケジューラ) System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive、Int32 toExclusive、ParallelOptions parallelOptions、Action1 body, Action
2 bodyWithState、Func4 bodyWithLocal, Func
1 localInit、Action1 localFinally) at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IList
1 list、ParallelOptions parallelOptions、Action1 body, Action
2 bodyWithState、Action3 bodyWithStateAndIndex, Func
4 bodyWithStateAndLocal、Func5 bodyWithEverything, Func
1 localInit、Action1 localFinally) at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable
1 source、ParallelOptions parallelOptions、Action1 body, Action
2 bodyWithState、Action3 bodyWithStateAndIndex, Func
4 bodyWithStateAndLocal、Func5 bodyWithEverything, Func
1 localInit、Action1 localFinally) at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable
C:\VS_apps\PMConsole\PMC Tools\ChangeOrderExecution\Program.cs:line 216 の ChangeOrder.Program.Main( String[] args) in C:\VS_apps\PMConsole\PMC Tools\ChangeOrderExecution\Program.cs: System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence) の行 1373 assemblySecurity, String[] args) で Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() で System.Threading.ThreadHelper.ThreadStart_Context(オブジェクト状態) で System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback コールバック, オブジェクト状態,System.Threading.ThreadHelper.ThreadStart() で System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback コールバック、オブジェクト状態) で Boolean ignoreSyncCtx) InnerException:
情報を取得しているだけなので、何をロックする必要があるのか わかりません(ROだけですよね?)。
「AsParallel」を追加することを考えましたが、それはクエリをそれ自体に対して並列に実行させるだけの PLINQ ディレクティブであると理解しています。
Parallel.ForEach ループ内で Linq クエリを実行している例を見つけることができないため、自分が何をしているのかさえわかりません。