public static void WaitUntilReady(ExecDelegate a, int sleepMs, string exceptionMessage)
        {
            bool isDone = false;

            while (!isDone)
            {
                System.Threading.Thread.Sleep(sleepMs);
                int result = a(out isDone);
                if (result < 0)
                {
                    throw new UPnPRendererExceptions(string.Format("{0}: {1}", exceptionMessage, result));
                }
            }
        }
Exemple #2
0
        public Form1(Controller c)
        {
            Controller = c;

            InitializeComponent();

            timerDblClick.Interval = SystemInformation.DoubleClickTime;

            LogHandler  = new LogDelegate(Log);
            ExecHandler = new ExecDelegate(Exec);

            _contextMenu = new System.Windows.Forms.ContextMenu();
            _itemStart   = _contextMenu.MenuItems.Add(Babelfish.Translate("Start Avatar"), new System.EventHandler(_menuStart_Click));
            _itemStop    = _contextMenu.MenuItems.Add(Babelfish.Translate("Stop Avatar"), new System.EventHandler(_menuStop_Click));
            _contextMenu.MenuItems.Add("-");
            _itemAutostart = _contextMenu.MenuItems.Add(Babelfish.Translate("Start with Windows"), new System.EventHandler(_menuShow_Autostart));
            if (Controller.ShowShow)
            {
                _itemShow = _contextMenu.MenuItems.Add(Babelfish.Translate("Show"), new System.EventHandler(_menuShow_Click));
            }
            _contextMenu.MenuItems.Add(Babelfish.Translate("Exit Tray Icon"), new System.EventHandler(_menuExit_Click));

            notifyIcon1.ContextMenu = _contextMenu;
        }
Exemple #3
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);
            });
        }
Exemple #4
0
 public static void WaitUntilReady(ExecDelegate a, int sleepMs, string exceptionMessage)
 {
   bool isDone = false;
   while (!isDone)
   {
     System.Threading.Thread.Sleep(sleepMs);
     int result = a(out isDone);
     if (result < 0)
       throw new UPnPRendererExceptions(string.Format("{0}: {1}", exceptionMessage, result));
   }
 }