How often in your project do you call Process.Start
? Unless it spins out far too many processes and you forget to explicitly dispose the relevant objects you might experience some memory pressure. There isn't any memory leak though, as objects that properly implement IDisposable
are ultimately cleaned up by the finalization process.
What should I keep in mind using Process.Start?
Is there anything that I need to be aware of such as memory leaks? I know that I should use statements such as MyProcess.Close and MyProcess.Dispose. Is there anything else I need to be aware of?
Developer technologies Windows Forms
Developer technologies VB
4 answers
Sort by: Most helpful
-
Lex Li (Microsoft) 6,037 Reputation points Microsoft Employee
2021-12-11T05:25:57.723+00:00 -
Karen Payne MVP 35,586 Reputation points Volunteer Moderator
2021-12-11T02:53:01.823+00:00 Use a Using block to clean-up memory e.g.
Using process As Process = Process.Start("TODO") 'Do whatever with the process End Using
Which closes and disposes or do dispose in a try/finally statement.
A real world example
Public Shared Sub UnblockFiles(ByVal folderName As String) If Not Directory.Exists(folderName) Then Return End If Dim start = New ProcessStartInfo With { .FileName = "powershell.exe", .Arguments = $"Get-ChildItem -Path '{folderName}' -Recurse | Unblock-File", .CreateNoWindow = True } Using process As System.Diagnostics.Process = System.Diagnostics.Process.Start(start) End Using End Sub
Rather than use WaitForExit use await WaitForExitAsync to keep your app responsive.
-
Marc Menzel 121 Reputation points
2021-12-11T03:32:34.317+00:00 What do you think about how I should approach this?
-
Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
2021-12-13T07:56:19.393+00:00 Hi @Marc Menzel ,
My project calls Process.Start everytime the user tells the program to open up a file or execute a program. When prcoesses exit, the program closes the process object created for the process and then disposes it.
It is recommended to use the using block to automatically call the dispose method of the process object at the end of the code segment.
Best Regards.
Jiachen Li----------
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.