private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            var monitorInfo = MonitorUtil.GetMachineMonitorInfo();
            var sites       = MonitorUtil.GetSiteInfos();

            ProcessMonitor.Init();
            ProcessMonitor.UpdateProcessList();
            var pl  = ProcessMonitor.ProcessList;
            var pl2 = pl.ToList().OrderByDescending(q => q.CpuUsage).ToList();
        }
        public static List <ProcessInfo> GetProcessInfos()
        {
            var processes     = Process.GetProcesses();
            var InstanceNames = ProcessMonitor.GetProcessInstanceNameList();

            List <ProcessMonitor> items = new List <ProcessMonitor>();

            foreach (var process in processes)
            {
                if (process.Id == 0)
                {
                    continue;
                }
                if (InstanceNames.Contains(process.ProcessName, StringComparer.OrdinalIgnoreCase))
                {
                    try
                    {
                        ProcessMonitor ProcessMonitor = new ProcessMonitor(process, InstanceNames);
                        ProcessMonitor.Init();
                        items.Add(ProcessMonitor);
                    }
                    catch (System.Exception) { }
                }
            }
            Thread.Sleep(10);
            List <ProcessInfo> processInfos = new List <ProcessInfo>();

            foreach (var item in items)
            {
                try
                {
                    item.UpdateInfo();
                    ProcessInfo info = new ProcessInfo()
                    {
                        Id           = item.Id,
                        FileName     = item.FileName,
                        ProcessName  = item.ProcessName,
                        InstanceName = item.InstanceName,
                        CpuUsage     = item.CpuUsage,
                        MemoryUsage  = item.MemoryUsage
                    };
                    processInfos.Add(info);
                }
                catch (System.Exception) { }
            }
            processInfos = processInfos.OrderByDescending(q => q.CpuUsage).ToList();
            return(processInfos);
        }
    public static void CheckIsOpen(string MUTEX_GUID, string HeadProgramName)
    {
        //string MUTEX_GUID = "czasopisma_coliber";

        Mutex oneMutex = null;

        try
        {
            oneMutex = Mutex.OpenExisting(MUTEX_GUID);
        }
        catch (WaitHandleCannotBeOpenedException Ex)
        {
            // Mutex nie istnieje, obsługa wyjątku
        }

        if (oneMutex == null)
        {
            oneMutex = new Mutex(true, MUTEX_GUID);
        }
        else
        {
            Process currentProcess = Process.GetCurrentProcess();

            String processName      = currentProcess.ProcessName;
            Int32  currentProcessId = currentProcess.Id;

            Process[] processes = Process.GetProcessesByName(processName);

            foreach (Process p in processes)
            {
                // Bring the existing instance to the front

                if (p.Id != currentProcessId)
                {
                    IntPtr windowHandle = p.MainWindowHandle;
                    SetForegroundWindow(windowHandle.ToInt32());
                }
            }

            oneMutex.Close();
            //return;
            //Thread.CurrentThread.Abort();
            Environment.Exit(0);
        }

        ProcessMonitor.Init("coliber");
    }
示例#4
0
        public static List <SiteProcessInfo> GetProcesses()
        {
            List <SiteProcessInfo> list = new List <SiteProcessInfo>();
            var server = new ServerManager();//请使用管理员模式

            foreach (Site site in server.Sites)
            {
                SiteProcessInfo siteInfo = new SiteProcessInfo()
                {
                    Name        = site.Name,
                    ProcessInfo = new Dictionary <string, ProcessInfo>()
                };
                foreach (var application in site.Applications)
                {
                    siteInfo.ProcessInfo[application.ApplicationPoolName] = new ProcessInfo();
                }
                list.Add(siteInfo);
            }
            foreach (var item in server.ApplicationPools)
            {
                foreach (WorkerProcess workerProcess in item.WorkerProcesses)
                {
                    var            t              = workerProcess.ProcessId;
                    var            processes      = Process.GetProcesses();
                    var            p              = processes.FirstOrDefault(q => q.Id == t);
                    ProcessMonitor processMonitor = new ProcessMonitor(p);
                    processMonitor.Init();
                    processMonitor.UpdateInfo();
                    foreach (var site in list)
                    {
                        if (site.ProcessInfo.TryGetValue(workerProcess.AppPoolName, out ProcessInfo processInfo))
                        {
                            processInfo.Id           = workerProcess.ProcessId;
                            processInfo.InstanceName = processMonitor.InstanceName;
                            processInfo.ProcessName  = processMonitor.ProcessName;
                            processInfo.FileName     = processMonitor.FileName;
                            processInfo.MemoryUsage  = processMonitor.MemoryUsage;
                            processInfo.CpuUsage     = processMonitor.CpuUsage;
                        }
                    }
                    processMonitor.Dispose();
                }
            }
            return(list);
        }