public static void processFiles(AssetUpdate data) { if (!enabled.value) { return; } var log = Log.@default; worker.EnqueueItem(() => { try { process(data, log); } catch (Exception e) { log.error(e); } }); }
static void Show() { PCQueue q = new PCQueue(2); Console.WriteLine("Enqueuing 10 items..."); for (int i = 0; i < 10; i++) { int itemNumber = i; // To avoid the captured variable trap q.EnqueueItem(() => { Thread.Sleep(1000); // Simulate time-consuming work Console.Write(" Task" + itemNumber); }); } q.Shutdown(true); Console.WriteLine(); Console.WriteLine("Workers complete!"); }
public void MonitorProducerConsumer() { const int workersCount = 10; const int workCount = 1000; int workDone = 0; PCQueue q = new PCQueue(workersCount); for (int i = 0; i < workCount; i++) { int itemNumber = i; q.EnqueueItem(() => { Thread.Sleep(1); Interlocked.Increment(ref workDone); }); } q.Shutdown(true); Assert.AreEqual(workCount, workDone); }
static void process( AssetUpdate data, int workers, Dictionary <string, string> pathToGuid, Dictionary <string, HashSet <string> > parents, Dictionary <string, HashSet <string> > children, Ref <float> progress, ILog log ) { progress.value = 0; Func <string, bool> predicate = p => p.EndsWithFast(".asset") || p.EndsWithFast(".prefab") || p.EndsWithFast(".unity") || p.EndsWithFast(".mat"); var assets = data.filter(predicate, _ => predicate(_.fromPath)); var firstScan = children.isEmpty() && parents.isEmpty(); var updatedChildren = firstScan ? children : new Dictionary <string, HashSet <string> >(); var progressIdx = 0; Action updateProgress = () => progress.value = (float)progressIdx / assets.totalAssets; var addedPool = new PCQueue(workers); foreach (var added in assets.newAssets) { updateProgress(); addedPool.EnqueueItem(() => { parseFile(pathToGuid, log, added, updatedChildren); lock (data) { progressIdx++; updateProgress(); } }); } // Block until parallel part is done. addedPool.Shutdown(waitForWorkers: true); foreach (var deleted in assets.deletedAssets) { updateProgress(); if (pathToGuid.ContainsKey(deleted)) { updatedChildren.getOrUpdate(pathToGuid[deleted], () => new HashSet <string>()); pathToGuid.Remove(deleted); } progressIdx++; } foreach (var moved in assets.movedAssets) { updateProgress(); foreach (var guid in pathToGuid.getAndRemove(moved.fromPath)) { pathToGuid[moved.toPath] = guid; } progressIdx++; } updateProgress(); if (!firstScan) { foreach (var parent in updatedChildren.Keys) { if (children.ContainsKey(parent)) { var set = children[parent]; foreach (var child in set) { parents[child].Remove(parent); } children.Remove(parent); } } } addParents(parents, updatedChildren); if (!firstScan) { foreach (var kv in updatedChildren) { if (kv.Value.Count > 0) { children.Add(kv.Key, kv.Value); } } } }
public void WriteToDisp(string stringIn) { WriteC.EnqueueItem(() => WriteThread(stringIn)); }