在不挂起我的工作流的情况下启动一个进程 VB.NET / C#
本文关键字:一个 进程 VB NET 我的 挂起 我的工作 工作流 启动 情况下 | 更新日期: 2023-09-27 18:32:13
我正在尝试构建一个"加载器" - 意思是,一个在我运行时将启动一些预定义程序的程序。问题是我不希望启动的程序中断我的工作流程,并且每个程序都有 0-2 秒的"挂起"。
If showLog = True Then Console.WriteLine("Starting --->" + "Skype")
proc.StartInfo.FileName = "C:'Program Files (x86)'Skype'Phone'Skype.exe"
proc.StartInfo.Arguments = "/nosplash /minimized"
proc.Start()
proc.PriorityClass = ProcessPriorityClass.BelowNormal
我想我也许能够以"空闲"或"低于正常"优先级启动该过程。但是我只能在程序加载后设置该优先级 - 为时已晚。
知道吗?
请注意,一切正常,但问题是"冻结"持续时间很小 - 当加载的程序少于几个时,这变得很重要。
谢谢。
一种选择是使用cmd/c start/low executable.exe参数启动Skype,这将在Skype.exe启动后完成,然后您可以按名称检索新的Skype.exe进程。
proc.StartInfo.FileName = "cmd.exe"
proc.StartInfo.Arguments = "/c start /low ""C:'Program Files (x86)'Skype'Phone'Skype.exe"" /nosplash /minimized"
proc.Start()
proc.WaitForExit()
proc = (From p In System.Diagnostics.Process.GetProcessesByName("skype.exe") Order By p.StartTime Descending).FirstOrDefault()
'
感谢迈克·德鲁克,我做到了:
(进程名称 = 不带".exe"的执行文件名 - "skype.exe" ===> "skype")
Private Sub StartProcessAndNormalize(Path As String, ProcessName As String, Optional Args As String = "", Optional PrintLog As Boolean = False)
If PrintLog = True Then Console.WriteLine("Starting --->" + ProcessName)
StartCmdProcess(Path, Args, ProcessName)
ProcessBackToNormal(ProcessName)
End Sub
Private Sub ProcessBackToNormal(ProcessName As String)
Dim proc As Process
Threading.Thread.Sleep(2000)
proc = (From p In System.Diagnostics.Process.GetProcessesByName(ProcessName) Order By p.StartTime Descending).FirstOrDefault()
Try
proc.PriorityClass = ProcessPriorityClass.Normal
Catch
Console.WriteLine("Can't find process " & ProcessName)
End Try
End Sub
Private Sub StartCmdProcess(Path As String, Args As String, Optional ProcessName As String = "")
Dim proc As Process = New Process
proc.StartInfo.FileName = "cmd.exe"
proc.StartInfo.Arguments = "/c start """ & ProcessName & """ /BelowNormal """ & Path & """ " & Args
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
proc.Start()
proc.WaitForExit()
End Sub
我们像这样使用它:
StartProcessAndNormalize("C:'Program Files (x86)'Skype'Phone'Skype.exe", "Skype", "/nosplash /minimized", showLog)