Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        public static void execInThread(string path, string args, string execDir, ExecDelegate callback, int timeout = 1000 * 100)
        {
            Async main = Async.runInThread(delegate(Async thread)
            {
                // Process result report class
                Process.Result res = new Process.Result();
                res.success        = false;
                res.code           = -1;
                res.output         = "";
                res.error          = "";
                res.execPath       = path;
                res.execArgs       = args;
                res.execDir        = execDir;
                // Create the process object
                using (var process = new System.Diagnostics.Process())
                {
                    // Set process options
                    process.StartInfo.FileName               = path;
                    process.StartInfo.Arguments              = args;
                    process.StartInfo.WorkingDirectory       = execDir;
                    process.StartInfo.CreateNoWindow         = true;
                    process.StartInfo.UseShellExecute        = false;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError  = true;
                    // Prepare for output reading
                    var output = new StringBuilder();
                    var error  = new StringBuilder();
                    // Set the output data pipes and callback
                    using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
                        using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
                        {
                            // Output buffer events
                            process.OutputDataReceived += (sender, e) =>
                            {
                                if (e.Data == null)
                                {
                                    outputWaitHandle.Set();
                                }
                                else
                                {
                                    output.AppendLine(e.Data);
                                }
                            };
                            // Error buffer event
                            process.ErrorDataReceived += (sender, e) =>
                            {
                                if (e.Data == null)
                                {
                                    errorWaitHandle.Set();
                                }
                                else
                                {
                                    error.AppendLine(e.Data);
                                }
                            };
                            // Actually start the process
                            process.Start();
                            // Start reading stderr/stdout
                            process.BeginOutputReadLine();
                            process.BeginErrorReadLine();
                            // Wait until correctly exited
                            var processDone = process.WaitForExit(timeout);
                            var outputDone  = outputWaitHandle.WaitOne(timeout);
                            var errorDone   = errorWaitHandle.WaitOne(timeout);
                            // Check result
                            if (processDone && outputDone && errorDone)
                            {
                                // Process completed.
                                res.code = process.ExitCode;
                            }
                            else
                            {
                                // Timed out.
                                res.code = -1;
                            }
                            // Read results
                            res.success = res.code == 0;
                            res.output  = output.ToString();
                            res.error   = error.ToString();
                        }
                }
                // Done
                thread.pushEvent("ExecDone", res);
            });

            main.onEvent("ExecDone", delegate(object datas)
            {
                Process.Result res = (Process.Result)datas;
                callback(res);
            });
        }