Skip to content
Snippets Groups Projects
Select Git revision
  • 6a45642b3db4f69cb470ba2eb09392e3624aea0d
  • RWTHmoodle_4.x default
  • reportlab-integration
  • master-update-ci
  • MOODLE_4-4.1.2
  • MOODLE_3.x
  • bash-version
  • amritad1991-master-patch-37910
  • master protected
  • master_backup
  • rohlfing-enhance-docs
  • rohlfing-patch-qr
  • v1.0.1
  • v1.0.0
  • v0.1.0
15 results

supplements.py

Blame
  • Forked from IENT / exam-scan
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    RunnableWrapper.cs 4.64 KiB
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Management;
    using System.Threading;
    
    namespace Coscine.ServiceWrapper
    {
        class RunnableWrapper
        {
            private Process process;
    
            private object __lockObj = new object();
    
            // Thread safe singleton
            private static RunnableWrapper instance = null;
            private static readonly object padlock = new object();
    
            private string executablePath = null;
    
            public static RunnableWrapper Instance
            {
                get
                {
                    lock (padlock)
                    {
                        if (instance == null)
                        {
                            instance = new RunnableWrapper();
                        }
                        return instance;
                    }
                }
            }
        
            private RunnableWrapper()
            {
    
            }
    
            private void KillAllProcesses(string exectuablePath)
            {
                var wmiQueryString = "SELECT ProcessId, ExecutablePath, CommandLine FROM Win32_Process";
                using (var searcher = new ManagementObjectSearcher(wmiQueryString))
                using (var results = searcher.Get())
                {
                    var query = from p in Process.GetProcesses()
                                join mo in results.Cast<ManagementObject>()
                                on p.Id equals (int)(uint)mo["ProcessId"]
                                select new
                                {
                                    Process = p,
                                    Path = (string)mo["ExecutablePath"],
                                    CommandLine = (string)mo["CommandLine"],
                                };
                    foreach (var item in query)
                    {
                        if(item.Path == exectuablePath)
                        {
                            if (!item.Process.HasExited)
                            {
                                if (!item.Process.HasExited)
                                {
                                    item.Process.Kill();
                                }
                            }
                        }
                    }
                }
            }
    
            public void Start(string[] args, string executablePath)
            {
                try
                {
                    this.executablePath = executablePath;
                    KillAllProcesses(executablePath);
    
                    if (process == null)
                    {
                        process = new Process();
                        process.StartInfo.UseShellExecute = false;
                        process.StartInfo.FileName = executablePath;
                        process.StartInfo.CreateNoWindow = true;
                        process.StartInfo.Arguments = string.Join(" ", args);
                        process.StartInfo.RedirectStandardInput = true;
                        process.StartInfo.RedirectStandardOutput = true;
                        process.StartInfo.RedirectStandardError = true;
                        //* Set your output and error (asynchronous) handlers
                        process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
                        process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);
                        process.Start();
                        process.BeginOutputReadLine();
                        process.BeginErrorReadLine();
                    }
                }
                catch (Exception e)
                {
                    File.AppendAllText(Path.GetTempPath() + "ServiceWrapper.log", $"{DateTime.Now}: {e.Message}{Environment.NewLine}");
                }
            }
    
            private void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
            {
                lock (__lockObj)
                {
                    //* Do your stuff with the output (write to console/log/StringBuilder)
                    File.AppendAllText(Path.GetTempPath() + "ServiceWrapper.log", $"{DateTime.Now}: {outLine.Data}{Environment.NewLine}");
                }
            }
    
            public void Stop()
            {
                try
                {
                    if (process != null && !process.HasExited)
                    {
                        process.StandardInput.WriteLine("\x3");
                        Thread.Sleep(1000);
                        process.StandardInput.Close();
                        Thread.Sleep(1000);
    
                        if (!process.HasExited)
                        {
                            process.Kill();
                        }
    
                        KillAllProcesses(this.executablePath);
                    }
                }
                catch (Exception e)
                {
                    File.AppendAllText(Path.GetTempPath() + "ServiceWrapper.log", $"{DateTime.Now}:{e.Message}{Environment.NewLine}");
                }
            }
        }
    }