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

时间:2021-07-17 20:45:05

Simple question: I want to open a URL using the Default Browser, so I just do Process.Start(url). However, I noticed that this returns an IDisposable object.

简单的问题:我想使用默认浏览器打开一个URL,所以我只做Process.Start(url)。但是,我注意到这会返回一个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很好,但我不想遇到一些难以调试的内存泄漏/资源耗尽问题导致我的程序保持不变对长时间浏览器进程的引用。

4 个解决方案

#1


9  

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? This would still allow a sort of "fire and forget" but not leave memory/resources in a bad state.

难道你不能把它包装在一个using子句中,以确保GC做任何它需要处理的事情如果你需要处理它吗?这仍然会允许某种“发射并忘记”但不会使内存/资源处于不良状态。

Probably overkill but there is a really good article on CodeProject about the IDisposable interface: http://www.codeproject.com/KB/dotnet/idisposable.aspx

可能有点矫枉过正,但有关IDisposable接口的CodeProject上有一篇非常好的文章:http://www.codeproject.com/KB/dotnet/idisposable.aspx

#2


10  

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上的Process.Start方法(字符串)(.NET Framework 4):

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客户端。)

#3


5  

Starting the process is a native call which returns a native process handle, which is stored in the instance of Process that is returned. 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实例中。 Process中有一些方法可以使用句柄,因此您可以执行等待进程退出或空闲的操作。

Disposing the Process frees that handle. I agree with Jon, wrap it in a using clause.

处理进程释放进程。我同意Jon,将其包含在using子句中。

#4


3  

@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. So if you don't manually dispose, the object may still be disposed within a few seconds of it going out of scope.
  • GC每次执行集合时,都会(最终)处理不再引用的对象。因此,如果您不手动处理,对象可能会在超出范围的几秒钟内被丢弃。

  • 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 (e.g. 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 (e.g. 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并且计算机的显示将失败),但一般来说,你很少会遇到这些情况,如果你遵循最佳实践(在不再需要时明确处理对象),你通常不需要知道这些情况何时发生,因为你已经正确地处理它们了。

#1


9  

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? This would still allow a sort of "fire and forget" but not leave memory/resources in a bad state.

难道你不能把它包装在一个using子句中,以确保GC做任何它需要处理的事情如果你需要处理它吗?这仍然会允许某种“发射并忘记”但不会使内存/资源处于不良状态。

Probably overkill but there is a really good article on CodeProject about the IDisposable interface: http://www.codeproject.com/KB/dotnet/idisposable.aspx

可能有点矫枉过正,但有关IDisposable接口的CodeProject上有一篇非常好的文章:http://www.codeproject.com/KB/dotnet/idisposable.aspx

#2


10  

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上的Process.Start方法(字符串)(.NET Framework 4):

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客户端。)

#3


5  

Starting the process is a native call which returns a native process handle, which is stored in the instance of Process that is returned. 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实例中。 Process中有一些方法可以使用句柄,因此您可以执行等待进程退出或空闲的操作。

Disposing the Process frees that handle. I agree with Jon, wrap it in a using clause.

处理进程释放进程。我同意Jon,将其包含在using子句中。

#4


3  

@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. So if you don't manually dispose, the object may still be disposed within a few seconds of it going out of scope.
  • GC每次执行集合时,都会(最终)处理不再引用的对象。因此,如果您不手动处理,对象可能会在超出范围的几秒钟内被丢弃。

  • 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 (e.g. 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 (e.g. 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并且计算机的显示将失败),但一般来说,你很少会遇到这些情况,如果你遵循最佳实践(在不再需要时明确处理对象),你通常不需要知道这些情况何时发生,因为你已经正确地处理它们了。