public ImageIterator(bool isRandom) { filenameIterators = Array.Empty <FilenameIterator>(); currentFilenameIteratorIndex = 0; currentEnumeratorInfo = null; this.isRandom = isRandom; }
private static async Task RunLoopAsync <T>( EnumeratorInfo <T> enumerator, T item, int index, ConcurrentQueue <Exception> exceptions, Func <T, int, CancellationToken, Task> func, CancellationToken cancelToken) { do { if (exceptions.Count > 0) { return; } try { await func(item, index, cancelToken).ConfigureAwait(false); } catch (Exception ex) { exceptions.Enqueue(ex); return; } }while (enumerator.TryGetNext(out item, out index)); }
public static IEnumerable <TResult> Merge <TItem, TResult>( this IReadOnlyList <IEnumerable <TItem> > sources, IComparer <TItem> comparer, Func <List <TItem>, TResult> merger) { var enumerators = new EnumeratorInfo <TItem> [sources.Count]; try { for (int i = 0; i < sources.Count; i++) { enumerators[i] = new EnumeratorInfo <TItem>(sources[i]?.OrderBy(x => x, comparer)); } var indexes = new List <int>(sources.Count); TResult result; while (MoveNext(enumerators, indexes, comparer, merger, out result)) { yield return(result); indexes.Clear(); } } finally { foreach (var item in enumerators) { item.Dispose(); } } }
public void OpenTimeSlice(EnumeratorInfo Start) { TimeSliceEnd = Start == null ? Util.EnvironmentTickCountAdd(MaxTimeSlice) : Util.EnvironmentTickCountAdd(MaxTimeSlice / 2); InTimeSlice = true; }
public void OpenTimeSlice(EnumeratorInfo Start) { TimeSliceStart = DateTime.Now; if (Start == null) { TimeSliceEnd = TimeSliceStart.AddMilliseconds(MaxTimeSlice); } else { TimeSliceEnd = TimeSliceStart.AddMilliseconds(MaxTimeSlice / 2); } InTimeSlice = true; }
public void SetImagePaths(string[] imagePaths, bool isRandom, ITheme theme) { if (imagePaths == null) { throw new ArgumentNullException(nameof(imagePaths)); } if (theme == null) { throw new ArgumentNullException(nameof(theme)); } var list = new List <FilenameIterator>(imagePaths.Length); foreach (var pathInfo in imagePaths) { if (pathInfo == null) { continue; } var path = pathInfo; string optionsString = string.Empty; int index = path.IndexOf('|'); if (index >= 0) { optionsString = path.Substring(0, index); path = path.Substring(index + 1); } path = path.Trim(); var sourceOptions = SourceOptions.Create(optionsString); if (HasAllowedUriScheme(path)) { list.Add(new FileIterator(path, sourceOptions)); } else if (File.Exists(path)) { list.Add(new FileIterator(path, sourceOptions)); } else if (Directory.Exists(path)) { list.Add(new DirectoryIterator(path, sourceOptions)); } } this.isRandom = isRandom; this.theme = theme; cachedAllFilenamesListWeakRef = null; currentEnumeratorInfo?.Dispose(); currentEnumeratorInfo = null; filenameIterators = list.ToArray(); currentFilenameIteratorIndex = 0; NextImageSource(); }
public static async Task WhenAll <T>( this IEnumerable <T> enumerable, CancellationToken cancelToken, Func <T, int, CancellationToken, Task> func, int?maxParallelization = null) { if (enumerable == null) { throw new ArgumentNullException(nameof(enumerable)); } if (func == null) { throw new ArgumentNullException(nameof(func)); } var exceptions = new ConcurrentQueue <Exception>(); var maxCount = maxParallelization ?? Environment.ProcessorCount; var tasks = new List <Task>(maxCount); using (var enumerator = new EnumeratorInfo <T>(enumerable)) { for (var i = 0; i < maxCount; i++) { T item; int index; if (!enumerator.TryGetNext(out item, out index)) { break; } var task = RunLoopAsync(enumerator, item, index, exceptions, func, cancelToken); tasks.Add(task); } await Task.WhenAll(tasks).ConfigureAwait(false); } if (exceptions.Count > 0) { throw new AggregateException(exceptions); } }
ImageInfo TryCreateNextImageSource(EnumeratorInfo info) { if (info == null) { return(null); } if (!info.Iterator.SourceOptions.IsSupportedTheme(theme)) { return(null); } while (info.Enumerator.MoveNext()) { var imgInfo = TryCreateImageSource(info.Enumerator.Current); if (imgInfo != null) { return(imgInfo); } } return(null); }
ImageInfo TryCreateNextImageSource() { if (filenameIterators.Length == 0) { return(null); } if (currentEnumeratorInfo == null) { currentEnumeratorInfo = new EnumeratorInfo(filenameIterators[currentFilenameIteratorIndex]); } var imgInfo = TryCreateNextImageSource(currentEnumeratorInfo); if (imgInfo != null) { return(imgInfo); } int baseIndex = currentFilenameIteratorIndex; // This loop will retry the current iterator again in case it has already returned // some filenames. for (int i = 0; i < filenameIterators.Length; i++) { currentEnumeratorInfo?.Dispose(); currentEnumeratorInfo = null; currentFilenameIteratorIndex = (i + 1 + baseIndex) % filenameIterators.Length; currentEnumeratorInfo = new EnumeratorInfo(filenameIterators[currentFilenameIteratorIndex]); imgInfo = TryCreateNextImageSource(currentEnumeratorInfo); if (imgInfo != null) { return(imgInfo); } } return(null); }
public EnumeratorInfo ExecuteEvent(string state, string FunctionName, object[] args, EnumeratorInfo Start, out Exception ex) { ex = null; return(null); }
public EnumeratorInfo ExecuteEvent(string state, string FunctionName, object[] args, EnumeratorInfo Start, out Exception ex) { ex = null; return null; }
public EnumeratorInfo ExecuteEvent(string state, string FunctionName, object[] args, EnumeratorInfo Start, out Exception ex) { return m_Executor.ExecuteEvent(state, FunctionName, args, Start, out ex); }
public void OpenTimeSlice(EnumeratorInfo Start) { TimeSliceEnd = Start == null ? Util.EnvironmentTickCountAdd(MaxTimeSlice) : Util.EnvironmentTickCountAdd(MaxTimeSlice/2); InTimeSlice = true; }
public EnumeratorInfo ExecuteEvent(string state, string FunctionName, object[] args, EnumeratorInfo Start, out Exception ex) { ex = null; string EventName = state == "" ? FunctionName : state + "_event_" + FunctionName; if (InTimeSlice) //MainConsole.Instance.Output("ScriptEngine TimeSlice Overlap " + FunctionName); return Start; IEnumerator thread = null; OpenTimeSlice(Start); bool running = true; try { if (Start != null) { lock (m_enumerators) m_enumerators.TryGetValue(Start.Key, out thread); } else thread = m_Script.FireEvent(EventName, args); if (thread != null) running = thread.MoveNext(); } catch (Exception tie) { // Grab the inner exception and rethrow it, unless the inner // exception is an EventAbortException as this indicates event // invocation termination due to a state change. // DO NOT THROW JUST THE INNER EXCEPTION! // FriendlyErrors depends on getting the whole exception! // if (!(tie is EventAbortException) && !(tie is MinEventDelayException) && !(tie.InnerException != null && ((tie.InnerException.Message.Contains("EventAbortException")) || (tie.InnerException.Message.Contains("MinEventDelayException"))))) ex = tie; if (Start != null) { lock (m_enumerators) { m_enumerators.Remove(Start.Key); } } CloseTimeSlice(); return null; } if (running && thread != null) { if (Start == null) { Start = new EnumeratorInfo {Key = UUID.Random().Guid}; } lock (m_enumerators) { m_enumerators[Start.Key] = thread; } if (thread.Current is DateTime) Start.SleepTo = (DateTime) thread.Current; else if (thread.Current is string) { ex = new Exception((string) thread.Current); running = false; lock (m_enumerators) { m_enumerators.Remove(Start.Key); } } CloseTimeSlice(); return Start; } else { //No enumerator.... errr.... something went really wrong here if (Start != null) { lock (m_enumerators) { m_enumerators.Remove(Start.Key); } } CloseTimeSlice(); return null; } }
public OrderedListItemLineInfo(EnumeratorInfo enumerator, IEnumerable<LineInfo> lines, SourceRange sourceRange) : base(lines, sourceRange) { Enumerator = enumerator; }
public EnumeratorInfo ExecuteEvent(string state, string FunctionName, object[] args, EnumeratorInfo Start, out Exception ex) { return(m_Executor.ExecuteEvent(state, FunctionName, args, Start, out ex)); }
public EnumeratorInfo ExecuteEvent(string state, string FunctionName, object[] args, EnumeratorInfo Start, out Exception ex) { ex = null; // IMPORTANT: Types and MemberInfo-derived objects require a LOT of memory. // Instead use RuntimeTypeHandle, RuntimeFieldHandle and RunTimeHandle (IntPtr) instead! string EventName = state + "_event_" + FunctionName; if (state == "") { EventName = FunctionName; } //#if DEBUG //m_log.Debug("ScriptEngine: Script event function name: " + EventName); //#endif #region Find Event // not sure it's need if (InTimeSlice) { //OpenSim.Framework.Console.MainConsole.Instance.Output("ScriptEngine TimeSlice Overlap" + FunctionName); return(Start); } MethodInfo ev = null; if (m_scriptType == null) { m_scriptType = m_Script.GetType(); } if (!Events.TryGetValue(EventName, out ev)) { // Not found, create ev = m_scriptType.GetMethod(EventName); Events.Add(EventName, ev); } if (ev == null) // No event by that event name! { //Attempt to find it just by name if (!Events.TryGetValue(EventName, out ev)) { // Not found, create ev = m_scriptType.GetMethod(FunctionName); Events.Add(FunctionName, ev); } if (ev == null) // No event by that name! { //m_log.Debug("ScriptEngine Can not find any event named:" + EventName); return(null); } } #endregion return(FireAsEnumerator(Start, ev, args, out ex)); }
public EnumeratorInfo ExecuteEvent(string state, string FunctionName, object[] args, EnumeratorInfo Start, out Exception ex) { ex = null; string EventName = state == "" ? FunctionName : state + "_event_" + FunctionName; if (InTimeSlice) { //MainConsole.Instance.Output("ScriptEngine TimeSlice Overlap " + FunctionName); return(Start); } IEnumerator thread = null; OpenTimeSlice(Start); bool running = true; try { if (Start != null) { lock (m_enumerators) m_enumerators.TryGetValue(Start.Key, out thread); } else { thread = m_Script.FireEvent(EventName, args); } if (thread != null) { running = thread.MoveNext(); } } catch (Exception tie) { // Grab the inner exception and rethrow it, unless the inner // exception is an EventAbortException as this indicates event // invocation termination due to a state change. // DO NOT THROW JUST THE INNER EXCEPTION! // FriendlyErrors depends on getting the whole exception! // if (!(tie is EventAbortException) && !(tie is MinEventDelayException) && !(tie.InnerException != null && ((tie.InnerException.Message.Contains("EventAbortException")) || (tie.InnerException.Message.Contains("MinEventDelayException"))))) { ex = tie; } if (Start != null) { lock (m_enumerators) { m_enumerators.Remove(Start.Key); } } CloseTimeSlice(); return(null); } if (running && thread != null) { if (Start == null) { Start = new EnumeratorInfo { Key = UUID.Random().Guid }; } lock (m_enumerators) { m_enumerators[Start.Key] = thread; } if (thread.Current is DateTime) { Start.SleepTo = (DateTime)thread.Current; } else if (thread.Current is string) { ex = new Exception((string)thread.Current); running = false; lock (m_enumerators) { m_enumerators.Remove(Start.Key); } } CloseTimeSlice(); return(Start); } else { //No enumerator.... errr.... something went really wrong here if (Start != null) { lock (m_enumerators) { m_enumerators.Remove(Start.Key); } } CloseTimeSlice(); return(null); } }
public EnumeratorInfo FireAsEnumerator(EnumeratorInfo Start, MethodInfo ev, object[] args, out Exception ex) { IEnumerator thread = null; OpenTimeSlice(Start); bool running = true; ex = null; try { if (Start != null) { lock (m_enumerators) { m_enumerators.TryGetValue(Start.Key, out thread); } } else { thread = (IEnumerator)ev.Invoke(m_Script, args); } if (thread != null) { running = thread.MoveNext(); } } catch (Exception tie) { // Grab the inner exception and rethrow it, unless the inner // exception is an EventAbortException as this indicates event // invocation termination due to a state change. // DO NOT THROW JUST THE INNER EXCEPTION! // FriendlyErrors depends on getting the whole exception! // if (!(tie is EventAbortException) && !(tie is MinEventDelayException)) { ex = tie; } if (Start != null) { lock (m_enumerators) { m_enumerators.Remove(Start.Key); } } CloseTimeSlice(); return(null); } if (running && thread != null) { if (Start == null) { Start = new EnumeratorInfo(); Start.Key = UUID.Random().Guid; } lock (m_enumerators) { m_enumerators[Start.Key] = thread; } if (thread.Current is DateTime) { Start.SleepTo = (DateTime)thread.Current; } else if (thread.Current is string) { ex = new Exception((string)thread.Current); running = false; lock (m_enumerators) { m_enumerators.Remove(Start.Key); } } CloseTimeSlice(); return(Start); } else { //No enumerator.... errr.... something went really wrong here if (Start != null) { lock (m_enumerators) { m_enumerators.Remove(Start.Key); } } CloseTimeSlice(); return(null); } }