【问题标题】:Event.BeginInvoke + Quartz.NET + FileSystemWatcher + Task.Run in loop = exception?Event.BeginInvoke + Quartz.NET + FileSystemWatcher + Task.Run in loop = 异常?
【发布时间】:2016-08-05 10:46:27
【问题描述】:

我浏览了 SO,找不到与我相当独特的问题相匹配的东西。

问题: 我一直在使用 Quartz.NET 和 FileSystemWatcher 玩一些代码,基本设置如下。

Quartz.NET 以 15 秒的间隔触发一个计时器。此触发器会导致在特定目录中创建文件名格式为 *.csv(或 *.xml - 只是一个示例)的文件。 FileSystemWatcher 会注意到该文件并执行一些代码,这反过来会在同一目录中输出一个 *.csv。现在我知道这会重新触发观察者,它会陷入无限循环——我曾经打算这样做。

当此代码运行时,我在管理事件的处理程序代码中收到越界异常。值得注意的是,在处理 Quartz 作业和使用 for 循环时会导致此异常。异常不会发生在foreach中

我意识到这段代码可能......很有趣,但是任何人都可以破译导致 for 中的额外增量(范围超出范围异常)以及为什么迭代器会成功?

美国东部时间 2016 年 4 月 14 日上午 8:05 更新

正如 Jon 指出的那样,这是一个简单的疏忽和睡眠不足。任务执行时i是共享资源。

                var tl = new Task[pdList.Count];
                for (int i = 0; i < pdList.Count; i++)
                {
                    int li = i; // i is shared...use local copy
                                // use foreach instead
                    tl[i] = Task.Factory.StartNew(() => { ExecuteProcess(pdList[li], hargs); }, TaskCreationOptions.LongRunning);
                }

更新时间:2016 年 4 月 14 日美国东部标准时间上午 7:09

根据 Jon 的指导,我将编写一小段代码,希望能复制该问题。同时,我添加了堆栈跟踪和异常详细信息。整个项目在 github 上免费提供:

https://github.com/banjoCoder/ThatIntegrationEngine/tree/Dev

异常信息

索引超出范围。必须是非负数且小于 集合。参数名称:索引

异常堆栈跟踪:

   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at ThatIntegrationEngine.Engine.<>c__DisplayClass25_1.<BeginProcessExecution>b__0() in Z:\src\ThatIntegrationEngine\ThatIntegrationEngine.Core\Hood\Engine.cs:line 265
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()

任务线程内的 Stacktrace 调用:

   at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
   at System.Environment.get_StackTrace()
   at ThatIntegrationEngine.Engine.<>c__DisplayClass25_1.<BeginProcessExecution>b__0() in Z:\src\ThatIntegrationEngine\ThatIntegrationEngine.Core\Hood\Engine.cs:line 263
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
   at System.Threading.Tasks.Task.ExecutionContextCallback(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
   at System.Threading.Tasks.Task.ExecuteEntry(Boolean bPreventDoubleExecution)
   at System.Threading.Tasks.ThreadPoolTaskScheduler.LongRunningThreadWork(Object obj)
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart(Object obj)
   at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
   at System.Environment.get_StackTrace()
   at ThatIntegrationEngine.Engine.<>c__DisplayClass25_1.<BeginProcessExecution>b__0() in Z:\src\ThatIntegrationEngine\ThatIntegrationEngine.Core\Hood\Engine.cs:line 263
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
   at System.Threading.Tasks.Task.ExecutionContextCallback(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
   at System.Threading.Tasks.Task.ExecuteEntry(Boolean bPreventDoubleExecution)
   at System.Threading.Tasks.ThreadPoolTaskScheduler.LongRunningThreadWork(Object obj)
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart(Object obj)
Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.dll

代码流程:

Quartz 触发器触发时引发的事件:

protected virtual void OnTimedActionAsync(object s, CronSchedulerEventArgs e)
{
    EventHandler<CronSchedulerEventArgs> handler = TimedActionAsync;
    handler?.BeginInvoke(this, e, cs =>
    {
        var delg = (EventHandler<CronSchedulerEventArgs>)cs.AsyncState;
        delg.EndInvoke(cs);
    },
    handler);
}

订阅 FSW 创建事件的内部类引发的异步事件

protected virtual void OnCreatedAsync(object sender, FileSystemEventArgs args)
{
    EventHandler<DirWatcherEventArgs> handler = FileArrivedAsync;
    handler?.BeginInvoke(this, new DirWatcherEventArgs(args.FullPath, WatcherChangeTypes.Created, Id, Name)
    , cs =>
        {
            var delg = (EventHandler<DirWatcherEventArgs>)cs.AsyncState;
            delg.EndInvoke(cs);
        }
    , handler);
}

事件终于处理完毕

    private void HandleScheduledActionAsync(object sender, CronSchedulerEventArgs csarg)
    {
        var scheduler = sender as ICronScheduler;
        if (scheduler != null)
        {
            // load process type based on trigger (sender) id
            // dynamically create instance of the process and pass 
            // all expected information
            BeginProcessExecution(csarg);
        }
    }

    private void HandleFileArrivalAsync(object sender, DirWatcherEventArgs dwargs)
    {
        var watcher = sender as IDirectoryWatcher;
        if (watcher != null)
        {
            // load process type based on trigger (sender) id
            // dynamically create instance of the process and pass 
            // all expected information
            BeginProcessExecution(dwargs);
        }
    }

    private void BeginProcessExecution(HandlerEventArgs hargs)
    {
        IList<IProcessDetails> pdList = null;

        if (this._processHandlerRelation.TryGetValue(hargs.TriggerId, out pdList))
        {
            if (pdList.Count > 1)
            {
                try
                {
                    //!!! FAILURE !!!  
                    //this fails with out of range exception
                    var tl = new Task[pdList.Count];
                    for(int i = 0; i < pdList.Count; i++)
                    {
                        //!!! EXCEPTION !!!  
                        // this causes an out of range exception
                        // at exec task is attempting to read final value of i                           
                        // is i accessed by ref? -- no it's a shared resource
                        tl[i] = Task.Factory.StartNew(() => ExecuteProcess(pdList[i], hargs), TaskCreationOptions.LongRunning);
                    }

                    //this does not cause an exception
                    //var tl = new List<Task>();
                    //foreach (var pd in pdList)
                    //{
                    //    tl.Add(Task.Factory.StartNew(() => ExecuteProcess(pd, hargs), TaskCreationOptions.LongRunning));
                    //}

                    Task.WaitAll(tl);

                    // this does not cause an exception
                    //Parallel.ForEach(pdList, pd => ExecuteProcess(pd, hargs));                     
                }
                catch (Exception e)
                {
                    // log exception
                }
            }
            else
            {// avoid parallelization if only one process is expected 
             // to be executed
                // execute on calling thread
                ExecuteProcess(pdList[0], hargs);
            }
        }
    }

【问题讨论】:

  • 确切的例外是什么,在哪里?你能在minimal reproducible example 中重现这个吗? (这里有很多代码......我希望你可以减少它而不会丢失问题。)
  • @JonSkeet 我将努力获得更小、可复制的代码。与此同时,我已经更新了帖子并提供了更多详细信息
  • @JonSkeet 我可以通过在 for 循环中分配一个本地 int 并将其传递给任务来绕过异常。似乎任务在执行时通过 ref 抓取 i ,到发生时,循环位于边界外的 N 处。我添加了一个注释,其中包含有效的代码
  • 与其说是“通过引用”,不如说是“它使用与您在别处更新的相同变量”。假设您使用的是 C# 5 或更高版本,最简单的解决方案是使用foreach 循环,因为您使用i 来索引pdList
  • 啊,是的。有道理,我没注意。代码的编写方式是共享资源。

标签: c# events asynchronous multitasking


【解决方案1】:

正如 Jon Skeet 指出的那样,这是一个简单的疏忽和睡眠不足。当任务执行时 i 是共享资源。

            var tl = new Task[pdList.Count];
            for (int i = 0; i < pdList.Count; i++)
            {
                int li = i; // i is shared...use local copy
                            // use foreach instead
                tl[i] = Task.Factory.StartNew(() => ExecuteProcess(pdList[li], hargs), TaskCreationOptions.LongRunning);
            }

工作解决方案:

                    int pi = 0;    
                    var plist = new Task[pdList.Count];                        
                    foreach(var pd in pdList)
                    {
                        plist[pi++] = Task.Factory.StartNew(() => ExecuteProcess(pd, hargs), TaskCreationOptions.LongRunning);
                    }

                    Task.WaitAll(plist);

【讨论】:

    猜你喜欢
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    • 2015-11-11
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    相关资源
    最近更新 更多