Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.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中执行Parallel.Foreach时,布尔包含已取消的任务异常#_C#_Asp.net_.net_Asp.net Mvc_Parallel.foreach - Fatal编程技术网

C# 在C中执行Parallel.Foreach时,布尔包含已取消的任务异常#

C# 在C中执行Parallel.Foreach时,布尔包含已取消的任务异常#,c#,asp.net,.net,asp.net-mvc,parallel.foreach,C#,Asp.net,.net,Asp.net Mvc,Parallel.foreach,我有一个奇怪的情况,我无法找到以下stacktrace的错误源: Message :One or more errors occurred.<br/> StackTrace : at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout

我有一个奇怪的情况,我无法找到以下stacktrace的错误源:

Message :One or more errors occurred.<br/>
StackTrace :   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally)
   at System.Threading.Tasks.Parallel.For(Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body)
这个问题的根源可能是什么?你们有什么建议给我我该去哪里找

@mortb这是我记录stacktrace异常的方式:

  string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Error.txt";

            using (StreamWriter writer = new StreamWriter(filePath, true))
            {
                writer.WriteLine("Message :" + ex.Message + "<br/>" + Environment.NewLine + "StackTrace :" + ex.StackTrace +
                   "" + Environment.NewLine + "Date :" + DateTime.Now.ToString());
                writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
            }
string filePath=Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+@“\Error.txt”;
使用(StreamWriter writer=newstreamwriter(filePath,true))
{
writer.WriteLine(“消息:+ex.Message+”
“+Environment.NewLine+”StackTrace:+ex.StackTrace+ “+Environment.NewLine+”日期:“+DateTime.Now.ToString()); writer.WriteLine(Environment.NewLine+“-----------------------------------------------------------------------------------------”+Environment.NewLine); }
您的
//内部代码正在引发异常。您看到的异常是
并行。For
循环终止其工作任务以正确关闭。通常情况下,
ForEach
函数应该在最后抛出一个异常,该异常包含错误的实际原因。您看到的错误看起来像是调试器触发了
ForEach
函数的内部异常。

建议的日志更改:

string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Error.txt";

using (StreamWriter writer = new StreamWriter(filePath, true))
{
      foreach(var inner in (ex as AggregateException).?InnerExceptions ?? (new [] {ex}))
      {
           writer.WriteLine("Message :" + inner.Message + "<br/>" + Environment.NewLine + "StackTrace :" + inner.StackTrace +
           "" + Environment.NewLine + "Date :" + DateTime.Now.ToString());
           writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
      }
}
string filePath=Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+@“\Error.txt”;
使用(StreamWriter writer=newstreamwriter(filePath,true))
{
foreach(var inner in(例如aggregateeexception)。?InnerExceptions???(新[]{ex}))
{
writer.WriteLine(“Message:+inner.Message+”
“+Environment.NewLine+”StackTrace:+inner.StackTrace+ “+Environment.NewLine+”日期:“+DateTime.Now.ToString()); writer.WriteLine(Environment.NewLine+“-----------------------------------------------------------------------------------------”+Environment.NewLine); } }
调试并查看抛出的异常属性
InnerExceptions
(注意
InnerExceptions
)外部异常只是“
//code-inside
”抛出的一个或多个异常的包装。实际的异常列在
InnerExceptions
@mortb中。这就是我的问题所在。。我无法将异常写入文本文件。。。等等,我会告诉你我是如何记录错误的。@mortb你能看到我更新的问题吗?这就是我记录异常本身的方式。。。有什么方法可以记录内部异常本身并查看哪一行抛出它吗?非常感谢您的回复!我已经更新了我的初始问题,我如何记录异常本身。。。有什么方法可以记录内部异常本身吗?异常的类型应该是
AggregateException
。如果您强制转换到此属性,则会得到一个属性
InnerExceptions
(请注意“s”)。这个列表中的一个例外就是你要找的。莫特,非常感谢你!!现在要尝试代码:)它说ex.不包含名为InnerException的属性,而只包含一个名为“InnerException”的singuar形式的属性。可能您捕获的异常是
catch(exception ex)
,然后它丢失了类型,我添加了代码将其转换为AggregateException(未测试)代码不是最优的,但它可能会起作用
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Error.txt";

using (StreamWriter writer = new StreamWriter(filePath, true))
{
      foreach(var inner in (ex as AggregateException).?InnerExceptions ?? (new [] {ex}))
      {
           writer.WriteLine("Message :" + inner.Message + "<br/>" + Environment.NewLine + "StackTrace :" + inner.StackTrace +
           "" + Environment.NewLine + "Date :" + DateTime.Now.ToString());
           writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
      }
}