简体   繁体   English

我必须处理 Process.Start(url) 吗?

[英]Do I have to Dispose Process.Start(url)?

Simple question: I want to open a URL using the Default Browser, so I just do Process.Start(url) .简单的问题:我想使用默认浏览器打开一个 URL,所以我只做Process.Start(url) However, I noticed that this returns an IDisposable object.但是,我注意到这会返回一个 IDisposable 对象。

So now I wonder if I have to dispose it?所以现在我想知道我是否必须处理它? Or, for that matter, if my Application is in any way responsible for this process?或者,就此而言,我的应用程序是否以任何方式负责此过程? The intended functionality is simply "Fire and forget", I do not want to have my application as a parent of the new process and it does not need to interact with it.预期的功能只是“即发即弃”,我不想让我的应用程序作为新进程的父进程,它不需要与之交互。

I've seen some similar but unrelated questions on SO that seem to say that simply calling Process.Start on a URL is fine, but I do not want to run into some hard to debug memory leaks/resource exhaustion issues caused my me program keeping references to long dead browser processes.我在 SO 上看到了一些类似但不相关的问题,似乎说简单地在 URL 上调用 Process.Start 就可以了,但我不想遇到一些难以调试的内存泄漏/资源耗尽问题导致我的程序保持对长期失效的浏览器进程的引用。

Couldn't you just wrap it in a using clause to ensure the GC does whatever it needs to do with it IF you are required to dispose of it?如果您需要处理它,难道您不能将它包装在using子句中以确保 GC 执行它需要做的任何事情吗? This would still allow a sort of "fire and forget" but not leave memory/resources in a bad state.这仍然会允许某种“即发即忘”,但不会使内存/资源处于不良状态。

Probably overkill but there is a really good article on CodeProject about the IDisposable interface: http://www.codeproject.com/KB/dotnet/idisposable.aspx可能有点矫枉过正,但在 CodeProject 上有一篇关于 IDisposable 接口的非常好的文章: http : //www.codeproject.com/KB/dotnet/idisposable.aspx

No, you do not.你不可以。

void Main()
{
    Process result = Process.Start("http://www.google.com");

    if (result == null)
    {
        Console.WriteLine("It returned null");
    }
}

Prints印刷

It returned null

From Process.Start Method (String) on MSDN (.NET Framework 4):从 MSDN(.NET Framework 4)上的Process.Start 方法(字符串):

If the address of the executable file to start is a URL, the process is not started and null is returned.如果要启动的可执行文件的地址是 URL,则不启动进程并返回 null。

(In general, though, the using statement is the right way to work with IDisposable objects. Except for WCF clients .) (不过,一般来说, using语句是处理 IDisposable 对象的正确方法。WCF 客户端除外。)

Starting the process is a native call which returns a native process handle, which is stored in the instance of Process that is returned.启动进程是一个本地调用,它返回一个本地进程句柄,该句柄存储在返回的 Process 实例中。 There are methods in Process that use the handle so you can do things like wait for the process to exit, or become idle. Process 中有使用句柄的方法,因此您可以执行诸如等待进程退出或空闲等操作。

Disposing the Process frees that handle.处理进程会释放该句柄。 I agree with Jon, wrap it in a using clause.我同意 Jon 的观点,将其包含在 using 子句中。

@Fooberichu's answer is spot on, but I think it's also worth pointing out that usually only a few things "need" to be explicitly disposed. @Fooberichu 的回答是正确的,但我认为还值得指出的是,通常只有少数事情“需要”明确处理。

Objects are always effectively disposed at some point:对象总是在某个时刻被有效地处理:

  • Any time the GC does a collection, it will (eventually) dispose of objects that are no longer referenced.每当 GC 进行收集时,它(最终)都会处理不再引用的对象。 So if you don't manually dispose, the object may still be disposed within a few seconds of it going out of scope.因此,如果您不手动处置,该对象可能仍会在其超出范围后的几秒钟内被处置。
  • When your application quits, all resources it held are released.当您的应用程序退出时,它持有的所有资源都将被释放。 (Although the objects may not be disposed by C#/.net, the OS will claim back pretty much anything your process grabbed. If the lifetime of the resource extends beyond your application, then the OS is usually responsible for cleaning it up) (尽管 C#/.net 可能不会处理这些对象,但操作系统会收回您的进程获取的几乎所有内容。如果资源的生命周期超出了您的应用程序,则操作系统通常负责清理它)

The point of manually disposing (or employing a 'using') is therefore not to ensure that the resources will be released, but to release them as early as possible .因此,手动处置(或使用“使用”)的目的不是确保资源被释放,而是尽可能早释放它们

These days you are unlikely to run out of most types of resources (eg memory, file handles, or system brushes).如今,您不太可能用完大多数类型的资源(例如内存、文件句柄或系统刷)。 However, if you hold on to resources when you don't need to, your program is likely to be less efficient, you might use more memory than necessary, or perhaps cause delays by temporarily blocking other applications from doing useful things, etc. In general, then, Disposing is about good etiquette, tidiness and cutting out unnecessary inefficiencies.但是,如果您在不需要时保留资源,您的程序可能会降低效率,您可能会使用不必要的内存,或者可能会通过暂时阻止其他应用程序执行有用的操作而导致延迟等。总的来说,处置是关于良好的礼仪、整洁和消除不必要的低效率。

There are a few cases where resources must be released (eg if you don't close a file, you can't open/rename/move/delete it from elsewhere in your program or other programs; if you keep allocating textures on your graphics card without releasing them, you'll run out of VRAM and the computer's display will fail), but in general, you will encounter these situations infrequently, and if you follow best practices (explicitly disposing objects when they're no longer needed), you won't usually need to know when these situations are occurring because you'll already be dealing with them correctly.在某些情况下必须释放资源(例如,如果您不关闭文件,则无法在程序或其他程序的其他位置打开/重命名/移动/删除它;如果您继续在图形上分配纹理卡而不释放它们,您将耗尽 VRAM 并且计算机的显示将失败),但一般来说,您将很少遇到这些情况,并且如果您遵循最佳实践(在不再需要对象时明确处理它们),您通常不需要知道这些情况何时发生,因为您已经在正确处理它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM