예제 #1
0
파일: Wait.cs 프로젝트: Glimmers/PosixSharp
 static void Wait(int pid)
 {
     try
     {
         Process.GetProcessById(pid).WaitForExit();
     }
     catch (ArgumentException e)
     {
         Stdio.perror("Can't wait on process: " + e.Message);
     }
 }
예제 #2
0
파일: Wait.cs 프로젝트: Glimmers/PosixSharp
        static void Wait(int[] pids)
        {
            Dictionary <int, Process> toWaitFor = new Dictionary <int, Process>();

            // sort through the list, getting actual processes, sending to error
            // processes that don't exist, etc.

            foreach (int pid in pids)
            {
                try
                {
                    Process p = Process.GetProcessById(pid);
                    p.EnableRaisingEvents = true;
                    toWaitFor.Add(pid, p);

                    // When the process' exit event triggers, remove it from the list of procs we're waiting on
                    p.Exited += (object o, EventArgs e) =>
                    {
                        if (toWaitFor.ContainsKey(pid))
                        {
                            toWaitFor.Remove(pid);
                        }
                    };

                    // Handle potential race condition for when the process finishes after we add it to the
                    // dict, but before we add the handler.
                    if (p.HasExited)
                    {
                        if (toWaitFor.ContainsKey(pid))
                        {
                            toWaitFor.Remove(pid);
                        }
                    }
                }
                catch (ArgumentException e)
                {
                    Stdio.perror("Can't wait on process " + pid + ": " + e.Message);

                    // Something had gone wrong, make sure we aren't going to be locking
                    // on a dead process
                    if (toWaitFor.ContainsKey(pid))
                    {
                        toWaitFor.Remove(pid);
                    }
                }
            }

            // Spinwait until all processes finish and remove themselves from the list

            System.Threading.SpinWait.SpinUntil(() => { return(toWaitFor.Count == 0); });
        }
예제 #3
0
        static int Unlink(string filename)
        {
            int retVal = 0;

            try
            {
                File.Delete(filename);
            }
            catch (Exception e)
            {
                Stdio.perror("Unable to delete file: " + e.Message);
                retVal = -1;
            }
            return(retVal);
        }
예제 #4
0
        static int MkDir(string toMake)
        {
            int retVal = 0;

            try
            {
                Directory.CreateDirectory(toMake);
            }
            catch (Exception e)
            {
                Stdio.perror("Unable to create directory: " + e.Message);
                retVal = -1;
            }
            return(retVal);
        }
예제 #5
0
        public static int Cd(string directory)
        {
            int retval = 0;

            try
            {
                Environment.CurrentDirectory = directory;
            }
            catch (Exception e)
            {
                Stdio.perror("Invalid directory:" + e.Message);
                retval = 1;
            }

            return(retval);
        }
예제 #6
0
        static int RmDir(string toDelete, bool recursive = false)
        {
            int retVal = 0;

            try
            {
                Directory.Delete(toDelete, recursive);
            }
            catch (Exception e)
            {
                Stdio.perror("Unable to delete directory: " + e.Message);
                retVal = -1;
            }

            return(retVal);
        }