与Parallel.Invoke()一起使用时出现System.AggregateException异常

时间:2013-02-06 16:26:13

标签: c# parallel-processing wmi

我正在尝试使用Parallel.Invoke()并行运行2个exe exe都试图访问相同的数据库(但不同的表)。此外,exe将在两个不同的服务器上运行。 我正在使用带有C#的WMI来启动远程服务器中的批处理文件(将调用其他exe文件)。

Parallel.Invoke(() =>
            {
                RunInParallel();
            },
            () =>
            {
                InvokeModule("EmiCalculator.exe");
            } 
            );

void RunInParallel()
{
                var connoptions = new ConnectionOptions();
                var managementScope = new ManagementScope
                    (String.Format(@"\\{0}\ROOT\CIMV2", REMOTE_COMPUTER), connoptions);
                var wmiProc = new ManagementClass(managementScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
                string commandLineInput = @"\\win-server1\D$\Data\Run.bat";
                var processToRun = new[] { commandLineInput };
                wmiProc.InvokeMethod("Create", processToRun);
}

Run.bat正在调用SalaryCalculator.exe

这两个exe都有一个带有ConnectionString的配置文件,如下所示。

<add name="CONN" connectionString="Data Source=SERVER1-LAB1;Initial Catalog=Loan;Integrated Security=True"/>

运行时,远程计算机中的exe崩溃并抛出异常:

Application: SalaryCalculator.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AggregateException
Stack:
   at System.Threading.Tasks.Task.WaitAll(System.Threading.Tasks.Task[], Int32, System.Threading.CancellationToken)
   at System.Threading.Tasks.Task.WaitAll(System.Threading.Tasks.Task[], Int32)
   at System.Threading.Tasks.Task.WaitAll(System.Threading.Tasks.Task[])

注意:如果我在连接到远程计算机后运行批处理文件,它运行正常。与Parallel.Invoke()

一起使用时,只会抛出错误

我该怎么办?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:-1)

我能够做到这一点。问题是连接字符串,因为提到了Integrated Secuity。 我刚刚更改了连接字符串以使用SQL server authentication而不是Windows身份验证。它运作良好。

< add name="CONN" connectionString="Data Source=SERVER1-LAB1;Initial Catalog=Loan;User Id=sa;Password=xxxx12345678" /> 
相关问题