public static Async runInMain(ActionDelegate fn) { Async main1Thread = new Async(true); Async main2Thread = new Async(true); main1Thread.proxy = main2Thread; main2Thread.proxy = main1Thread; Nanome.Core.Daemon.Dispatcher.queue(delegate() { main2Thread.pushEvent("ASYNC-START", null); main2Thread.pushEvent("ASYNC-START-THREADED", null); try { fn(main2Thread); } catch (Exception exc) { Logs.errorOnChannel("Nanome.Core", "Error in main thread", exc); } main2Thread.pushEvent("ASYNC-DONE", null); main2Thread.pushEvent("ASYNC-DONE-THREADED", null); }); return(main1Thread); }
public static Async runInThread(ActionDelegate fn) { // If we are debugging, run threads in the main thread if (debugInMain) { return(runInMain(fn)); } // Otherwise proceed normally in the threadpool Async mainThread = new Async(true); Async poolThread = new Async(false); mainThread.proxy = poolThread; poolThread.proxy = mainThread; // Count thread scheduling lock (counterLock) { counter++; if (debugPlots) { if (firstTime == 0f) { firstTime = DateTime.Now.ToTimestamp(); } DataPlotter.AddDataPoint("Asyncs", "Async", (float)(DateTime.Now.ToTimestamp() - firstTime), counter); } } // Don't run the thread if things are going out of hand var running = true; lock (counterLock) { running = counter < 256; } if (running) { // Schedule thread Nanome.Core.Daemon.ThreadPool.queue(delegate() { poolThread.pushEvent("ASYNC-START", null); poolThread.pushEvent("ASYNC-START-THREADED", null); try { fn(poolThread); } catch (Exception exc) { Logs.errorOnChannel("Nanome.Core", "Error in a thread", exc); } poolThread.pushEvent("ASYNC-DONE", null); poolThread.pushEvent("ASYNC-DONE-THREADED", null); lock (counterLock) { counter--; if (debugPlots) { DataPlotter.AddDataPoint("Asyncs", "Async", (float)(DateTime.Now.ToTimestamp() - firstTime), counter); } } }); } else { Logs.errorOnChannel("Nanome.Core", "Too much threading load", "bailing out"); } return(mainThread); }