Example #1
0
 public Adapter()
 {
     answer = new int[4];
     try
     {
         process = new System.Diagnostics.Process();
         process.StartInfo.CreateNoWindow         = true;
         process.StartInfo.ErrorDialog            = false;
         process.StartInfo.FileName               = "PortfishNet40.exe";
         process.StartInfo.RedirectStandardOutput = true;
         process.StartInfo.UseShellExecute        = false;
         process.StartInfo.RedirectStandardInput  = true;
         process.StartInfo.RedirectStandardError  = true;
         process.StartInfo.WindowStyle            = System.Diagnostics.ProcessWindowStyle.Hidden;
         process.OutputDataReceived              += new System.Diagnostics.DataReceivedEventHandler(p_OutputDataReceived);
         process.Exited += new EventHandler(p_Exited);
         using (ChangeErrorMode newnewerr = new ChangeErrorMode(ChangeErrorMode.ErrorModes.FailCriticalErrors | ChangeErrorMode.ErrorModes.NoGpFaultErrorBox))
             process.Start();
         process.EnableRaisingEvents = true;
         process.BeginOutputReadLine();
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
     Send("uci");
 }
Example #2
0
        private static void IndexImpl(string databaseMetaTablePath, IEnumerable <string> videoPaths, int numThreads, long maxMemory)
        {
            IEnumerable <string> knownHashes = GetKnownDatabaseEntries(databaseMetaTablePath);
            var skippedFiles = new ConcurrentBag <string>();

            using (ChangeErrorMode _ = new ChangeErrorMode(ChangeErrorMode.ErrorModes.FailCriticalErrors | ChangeErrorMode.ErrorModes.NoGpFaultErrorBox))
                using (FingerPrintStore store = new FingerPrintStore(databaseMetaTablePath))
                {
                    long maxMemoryPerIndexJob = (long)Math.Round((double)maxMemory / numThreads);
                    Parallel.ForEach(
                        videoPaths.Except(knownHashes),
                        new ParallelOptions {
                        MaxDegreeOfParallelism = numThreads
                    },
                        videoPath =>
                    {
                        string fileName = Path.GetFileName(videoPath);
                        try
                        {
                            if (videoPath.Length > 260) // Max file path that mediainfo can handle
                            {
                                Console.WriteLine("File path is too long. Skipping: " + fileName);
                                return;
                            }

                            VideoFingerPrintWrapper videoFingerPrint = Video.VideoIndexer.IndexVideo(videoPath, maxMemoryPerIndexJob);
                            store.AddFingerPrint(videoFingerPrint);
                        }
                        catch (InvalidOperationException e)
                        {
                            Console.WriteLine(string.Format("Unable to hash file {0}. Reason: {1}. Skipping", fileName, e.Message));
                            skippedFiles.Add(videoPath);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(string.Format("Unable to hash file {0}. Reason: {1}. Skipping", fileName, e.Message));
                        }
                    }
                        );

                    // Attempting to index skipped files
                    foreach (string skippedFile in skippedFiles)
                    {
                        Console.WriteLine("Attempting to hash skipped file: " + skippedFile);
                        try
                        {
                            VideoFingerPrintWrapper videoFingerPrint = Video.VideoIndexer.IndexVideo(skippedFile, maxMemory);
                            store.AddFingerPrint(videoFingerPrint);
                        }
                        catch (InvalidOperationException)
                        {
                            Console.WriteLine(string.Format("Unable to hash file {0} for the second time. Giving up", skippedFile));
                        }
                    }

                    store.Shutdown();
                    store.Wait();
                }
        }
        private void StartProcess(Process p)
        {
            using (ChangeErrorMode newErrorMode = new ChangeErrorMode(ChangeErrorMode.ErrorModes.FailCriticalErrors | ChangeErrorMode.ErrorModes.NoGpFaultErrorBox))
            {
                p.Start();
                p.BeginOutputReadLine();
                p.BeginErrorReadLine();

                var didExit = p.WaitForExit(Settings.Default.MoveTimeoutSeconds * 1000);
                _botTimer.Stop();

                if (!didExit)
                {
                    if (!p.HasExited)
                        p.Kill();
                    OutputAppendLog(String.Format("[GAME]\tBot {0} timed out after {1} ms.", PlayerName,
                        _botTimer.ElapsedMilliseconds));
                    OutputAppendLog(String.Format("[GAME]\tKilled process {0}.", _processName));
                }
                else
                {
                    OutputAppendLog(String.Format("[GAME]\tBot {0} finished in {1} ms.", PlayerName,
                        _botTimer.ElapsedMilliseconds));
                }

                if ((didExit) && (p.ExitCode != 0))
                {
                    OutputAppendLog(String.Format("[GAME]\tProcess exited with non-zero code {0} from player {1}.",
                        p.ExitCode, PlayerName));
                }
            }
        }
Example #4
0
        // Based on: https://gist.github.com/georg-jung/3a8703946075d56423e418ea76212745
        public async Task <int> Run()
        {
            // Sanity check
            if (status == null)
            {
                return(-1);
            }

            using (status.proc = new Process())
            {
                status.proc.StartInfo.FileName        = this.cmd;
                status.proc.StartInfo.Arguments       = this.opt;
                status.proc.StartInfo.UseShellExecute = false;
                status.proc.StartInfo.ErrorDialog     = false;

                //status.proc.StartInfo.RedirectStandardInput = true;
                status.proc.StartInfo.RedirectStandardOutput = true;
                status.proc.StartInfo.RedirectStandardError  = true;
                status.proc.StartInfo.CreateNoWindow         = true;
                status.proc.EnableRaisingEvents = true;

                TaskCompletionSource <bool> out_done = new TaskCompletionSource <bool>();
                TaskCompletionSource <bool> err_done = new TaskCompletionSource <bool>();

                status.proc.Exited += (s, e) => {
                    status.running = false;
                };

                status.proc.Disposed += (s, e) => {
                    status.running = false;
                };

                status.proc.OutputDataReceived += (s, e) => {
                    if (e.Data == null)
                    {
                        out_done.SetResult(true);
                    }
                    else if (OutputCallback != null)
                    {
                        OutputCallback(e.Data);
                    }
                };

                status.proc.ErrorDataReceived += (s, e) => {
                    if (e.Data == null)
                    {
                        err_done.SetResult(true);
                    }
                    else if (ErrorCallback != null)
                    {
                        ErrorCallback(e.Data);
                    }
                };

                lock (this)
                {
                    using (ChangeErrorMode mode = new ChangeErrorMode(ErrorModes.SEM_FAILCRITICALERRORS | ErrorModes.SEM_NOGPFAULTERRORBOX))
                    {
                        status.exit_event.Reset();
                        if (!status.proc.Start())
                        {
                            status.running = false;
                            status.exit_event.Set(); // ?
                            return(-1);
                        }
                        status.running = true;
                    }
                }

                // Reads the output stream first and then waits because deadlocks are possible
                status.proc.BeginOutputReadLine();
                status.proc.BeginErrorReadLine();

                // Creates task to wait for process exit using timeout
                Task <bool> timeout_done = Task.Run(() => status.proc.WaitForExit(this.timeout));

                // Create task to wait for process exit and closing all output streams
                Task <bool[]> process_done = Task.WhenAll(timeout_done, out_done.Task, err_done.Task);

                // Waits process completion and then checks it was not completed by timeout
                if (await Task.WhenAny(Task.Delay(timeout), process_done) == process_done && timeout_done.Result)
                {
                    status.exit_code = status.proc.ExitCode;
                }
                else
                {
                    try { status.proc.Kill(); }
                    catch {}
                }
            } // using process

            status.exit_event.Set();
            return(status.exit_code);
        }
 private void StartProcessWindows(Process p)
 {
     // Prevent silly error has occurred dialogs on Windows...
     using (ChangeErrorMode newErrorMode = new ChangeErrorMode(ChangeErrorMode.ErrorModes.FailCriticalErrors | ChangeErrorMode.ErrorModes.NoGpFaultErrorBox))
     {
         StartProcessCommon (p);
     }
 }