我需要担心 foreach 循环中的 Process 吗

发布于 2024-10-18 20:08:46 字数 489 浏览 1 评论 0原文

这是一段代码,它运行所有进程,当它找到正确的进程时,代码会发送消息。我的问题是“proc”发生了什么,如何处理该进程。

//get all other (possible) running instances
        Process[] processes = Process.GetProcesses();            
        foreach (Process proc in processes)
        {
            if (proc.ProcessName.ToLower() == ProcessName.ToLower())
            {
                SendMessage(proc.MainWindowHandle, (uint)Message, IntPtr.Zero, IntPtr.Zero);
            }               
        }

提前致谢, 戒日

Here is the piece of code, which run through all the process and when It finds the right process, code sends the message. My question is what happened to the 'proc', how to dispose that process.

//get all other (possible) running instances
        Process[] processes = Process.GetProcesses();            
        foreach (Process proc in processes)
        {
            if (proc.ProcessName.ToLower() == ProcessName.ToLower())
            {
                SendMessage(proc.MainWindowHandle, (uint)Message, IntPtr.Zero, IntPtr.Zero);
            }               
        }

Thanks in advance,
Harsha

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不顾 2024-10-25 20:08:46

为了确保尽早释放所有资源,请在不再需要该进程时调用 Dispose。

//get all other (possible) running instances
Process[] processes = Process.GetProcesses();
try
{
    foreach (Process proc in processes)
    {
    // use proc
    }
}
finally
{
    foreach (Process proc in processes)
        proc.Dispose();
    processes = null;
}

To make sure all resoucers are freed as early as possible, call Dispose on the process, when you no longer need it.

//get all other (possible) running instances
Process[] processes = Process.GetProcesses();
try
{
    foreach (Process proc in processes)
    {
    // use proc
    }
}
finally
{
    foreach (Process proc in processes)
        proc.Dispose();
    processes = null;
}
神经暖 2024-10-25 20:08:46

一般来说,您无需担心对象的处置或解除分配,除非该对象实现了 IDisposable 接口。如果确实如此,您应该在完成后手动调用它的 Dispose() 方法,或者用 using 语句包装以自动调用它:

using (var disposableObject = new DisposableType())
{
    // do work with disposableObject
}

In general terms you don't need to worry about disposing or deallocating objects, unless the object implements the IDisposable interface. If it does you should either call the Dispose() method on it manually when you're finished, or wrap with a using statement to have it called automatically:

using (var disposableObject = new DisposableType())
{
    // do work with disposableObject
}
少女七分熟 2024-10-25 20:08:46

如果您正在循环查找您赢得的进程,那么您可以尝试类似的操作:

Process.GetCurrentProcess();

无论如何,我会将其更改为:

    foreach (Process proc in Process.GetProcesses())
    {
        if (proc.ProcessName.ToLower() == ProcessName.ToLower())
        {
            SendMessage(proc.MainWindowHandle, (uint)Message, IntPtr.Zero, IntPtr.Zero);
        }               
    }

这样就没有变量会引用“GetProcesses”,并且 GC 最终会处理它。

IF you are looping to find your won process then you could try something like:

Process.GetCurrentProcess();

In any case I would change it to:

    foreach (Process proc in Process.GetProcesses())
    {
        if (proc.ProcessName.ToLower() == ProcessName.ToLower())
        {
            SendMessage(proc.MainWindowHandle, (uint)Message, IntPtr.Zero, IntPtr.Zero);
        }               
    }

That way no variable will refernce the "GetProcesses" and the GC would eventually handle it.

微凉徒眸意 2024-10-25 20:08:46

变量 proc 是 foreach 循环的本地变量,因此一旦循环完成,它将自动被垃圾收集。

The variable proc is local to the foreach loop so once the loop completes, it will automatically be garbage collected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文