private void InitFromTasks(List <TaskInfo> infoList) { m_TaskInfoList = infoList; m_TaskExecutorList = new List <IExecuteTask>(infoList.Count); bool needAllFileWatch = false; foreach (TaskInfo info in infoList) { IExecuteTask tmp; if (info.IsExe) { tmp = new ExeTaskExecutor(FolderPath, info); } else if (info.IsSp) { tmp = new SpTaskExecutor(FolderPath, info); } else if (info.WatchFileList == null || info.WatchFileList.RelativePath == null || info.WatchFileList.RelativePath.Count <= 0) { tmp = new TaskExecutor(FolderPath, info); } else { tmp = new AppDomainTaskExecutor(FolderPath, info); needAllFileWatch = true; } tmp.UnhandledException += (s, e) => { OnUnhandledException(s, e); }; tmp.ExecutedNotify += (s, e) => { OnExecutedNotify(s, e); }; m_TaskExecutorList.Add(tmp); } if (needAllFileWatch) { m_FileWatcher = new FileWatcher(FolderPath, FilesChanged); } else { m_FileWatcher = new FileWatcher(FolderPath, Path.GetFileName(m_TaskConfigMgr.ConfigFilePath), FilesChanged); } }
internal static void DomainCallBack() { TaskInfo t = (TaskInfo)AppDomain.CurrentDomain.GetData("TaskInfo"); string folderPath = AppDomain.CurrentDomain.GetData("DllFolderPath").ToString(); AppDomain.CurrentDomain.SetData("APPBASE", folderPath); AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", Path.Combine(folderPath, "app.config")); AppDomainTaskExecutor handler = (AppDomainTaskExecutor)AppDomain.CurrentDomain.GetData("ExceptionHandler"); AppDomain.CurrentDomain.UnhandledException += (s, e) => { if (e.ExceptionObject is System.Exception) { Exception ex1 = (Exception)e.ExceptionObject; string desc = "AppDomain的UnhandledException事件被触发"; Logger.Error(new ExceptionEventArgs(ex1.ToString(), folderPath, t.ID, desc)); try { handler.OnUnhandledException(ex1.ToString(), desc); } catch (Exception ex2) { Logger.Error(folderPath, t.ID, ex2, "执行AppDomain.CurrentDomain.UnhandledException的OnUnhandledException时出错"); } } }; AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { string path = args.Name.Split(',')[0].Trim() + ".dll"; path = Path.Combine(AppDomain.CurrentDomain.GetData("DllFolderPath").ToString(), path); if (File.Exists(path)) { try { return(Assembly.LoadFrom(path)); } catch (Exception ex) { var de = new ExceptionEventArgs(ex, folderPath, t.ID, "Failed to load assembly \"" + path + "\" in AppDomain.CurrentDomain.AssemblyResolve."); Logger.Error(de); try { handler.OnUnhandledException(de); } catch (Exception ex1) { Logger.Error(folderPath, t.ID, ex1, "执行AppDomain.CurrentDomain.AssemblyResolve的OnUnhandledException时出错"); } } } return(null); }; TaskExecutor executor = new TaskExecutor(folderPath, t); executor.UnhandledException += (s, e) => { handler.OnUnhandledException(e); }; executor.ExecutedNotify += (s, e) => { handler.OnExecutedNotify(e); }; AppDomain.CurrentDomain.SetData("Executor", executor); }