【问题标题】:Why when adding processes to dataGridView1 i see only 9 processes?为什么向 dataGridView1 添加进程时我只看到 9 个进程?
【发布时间】:2014-06-05 15:00:35
【问题描述】:

当我运行 Windows rask 管理器时,我看到许多进程/应用程序。 但在我的程序中,我只看到 9 个。

void PopulateApplications()
        {
            dataGridView1.Rows.Clear();           
            foreach (Process p in Process.GetProcesses())
            {
                if (p.MainWindowTitle.Length > 1)
                {
                    var icon = Icon.ExtractAssociatedIcon(p.MainModule.FileName);
                    ima = icon.ToBitmap();
                    ima = resizeImage(ima, new Size(25, 25));
                    ima.Save(@"c:\temp\ima.jpg");
                    String status = p.Responding ? "Running" : "Not Responding";
                    dataGridView1.Rows.Add(ima,p.ProcessName, status);
                    DoubleBuffered1(dataGridView1, true);
                }
            }
            firsttime += 1;
            if (firsttime == 1)
            {
                NumberOfRows = dataGridView1.Rows.Count;
            }
            if (NumberOfRows != dataGridView1.Rows.Count)
            {
                int diff = dataGridView1.Rows.Count - NumberOfRows;
                this.Height = this.Height + (ROW_SIZE * diff);
                NumberOfRows = dataGridView1.Rows.Count;
            }

        }

我遍历进程并为每个进程添加一行,它是图标。 但我在 dataGridView1 中只看到 9 个进程。

如何像在任务管理器中一样将所有进程/应用程序添加到 dataGridView1 行? 我如何才能找到系统一直在运行的那些我无法关闭但正在运行的进程列表?

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    因为许多进程是后台进程/在没有附加任何窗口的情况下运行,所以 MainWindowTitle 将为空(string.Empty 是正确的),并且您的测试 if (p.MainWindowTitle.Length > 1) 将为所有这些进程返回 false

    MSDN 说:

    如果关联进程没有主窗口(因此 MainWindowHandle 为零),则 MainWindowTitle 为空字符串(“”)。

    【讨论】:

    • quantdev 那么当我运行它时 Windows rask 管理器会显示什么?他展示了很多进程/应用程序。
    • 你看过Process.ProcessName 吗?你能确认问题已解决吗?
    • 是的,那么我该如何获取所有图标?
    • 我在这里使用 ProcessName:dataGridView1.Rows.Add(ima,p.ProcessName, status);
    • 干脆不做测试if (p.MainWindowTitle.Length > 1)。您还应该在检索图标之前测试字符串p.MainModule.FileName,有些进程没有图标。请记住接受答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多