How to get the current ProcessID?

后端 未结 3 1443
囚心锁ツ
囚心锁ツ 2020-12-02 22:10

What\'s the simplest way to obtain the current process ID from within your own application, using the .NET Framework?

3条回答
  •  鱼传尺愫
    2020-12-02 22:30

    The upcoming .NET 5 introduces Environment.ProcessId which should be preferred over Process.GetCurrentProcess().Id as it avoids allocations and the need to dispose the Process object.

    https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-5/ shows a benchmark where Environment.ProcessId only takes 3ns instead of 68ns with Process.GetCurrentProcess().Id.

提交回复
热议问题