Event.BeginInvoke + Quartz.NET + FileSystemWatcher + Task.Run in loop = exception?

时间:2016-04-14 03:59:22

标签: c# events asynchronous multitasking

我看了看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

异常信息

  

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

异常Stacktrace:

   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);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

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);