繁体   English   中英

无法最小化 Process.Start 上的新进程 Window

[英]Can't minimize the new Process Window on Process.Start

我需要从我的程序中启动一个 java .jar进程。
一旦开始,我得到 output 并处理它。

为此,我使用以下代码:

Dim p_Process As New Process()
Dim p_p As New ProcessStartInfo
p_p.FileName = "java.exe"
p_p.Arguments = "-jar myjar.jar"

p_p.WorkingDirectory = apppath & "\myFolder"

p_p.UseShellExecute = False
p_p.RedirectStandardOutput = True
p_p.WindowStyle = ProcessWindowStyle.Minimized
AddHandler p_Process.OutputDataReceived, AddressOf manageContent

p_Process.StartInfo = p_p
p_Process.Start()
p_Process.BeginOutputReadLine()

我需要以下几行才能获得进程的 output 供我使用:

p_p.UseShellExecute = False
p_p.RedirectStandardOutput = True

一切正常,但 window 没有最小化。
如何最小化 window?

一种可能的方法是最小化 Java.exe 生成的Java.exe ,当它出现时,使用 UI 自动化。
当进程启动时,执行Jar文件并创建新的 Window。 This Window has a specific class name, SunAwtFrame : this value can be used to identify the window, then access the UI Automation Element's WindowPattern and call its SetWindowVisualState() method to minimize the Window.

您也可以使用 Window Title 属性,以防它在这里是更好的选择。 在这种情况下,识别 Window 的PropertyConditionNameProperty而不是ClassNameProperty

window = AutomationElement.RootElement.FindFirst(TreeScope.Children, New PropertyCondition(
    AutomationElement.NameProperty, "[The Window Title]"))

当然,该过程是完美的功能。

在这里,我使用异步变体实现它,重定向 StandardOutput 和 StandardError,还启用和订阅Exited事件,设置[Process].EnableRaisingEvents = True
Exited事件会在进程关闭时发出通知,并且还会处理进程 object。


此处的代码使用 StopWatch 来等待 Process' Window 出现,因为Process.WaitForInputIdle()可能无法正确完成并过早完成。
如果3000毫秒的间隔太短或太长,请调整代码。
但是请注意,只要 Window 出现,就会退出While循环。

此代码需要对UIAutomationClientUIAutomationTypes程序集的引用。

Imports System.Windows.Automation

Dim proc As New Process()
Dim psInfo As New ProcessStartInfo() With {
    .FileName = "java.exe",
    .Arguments = "-jar YourJarFile.jar",
    .WorkingDirectory = "[Your Jar File Path]",
    .UseShellExecute = False,
    .RedirectStandardOutput = True,
    .RedirectStandardError = True
}

proc.EnableRaisingEvents = True
proc.StartInfo = psInfo

AddHandler proc.OutputDataReceived,
    Sub(o, ev)
        Console.WriteLine(ev.Data?.ToString())
    End Sub
AddHandler proc.ErrorDataReceived,
    Sub(o, ev)
        Console.WriteLine(ev.Data?.ToString())
    End Sub
AddHandler proc.Exited,
    Sub(o, ev)
        Console.WriteLine("Process Exited")
        proc?.Dispose()
    End Sub

proc.Start()
proc.BeginOutputReadLine()

Dim window As AutomationElement = Nothing
Dim sw1 As Stopwatch = New Stopwatch()
sw1.Start()
While True
    window = AutomationElement.RootElement.FindFirst(
        TreeScope.Children, New PropertyCondition(
        AutomationElement.ClassNameProperty, "SunAwtFrame"))
    If window IsNot Nothing OrElse sw1.ElapsedMilliseconds > 3000 Then Exit While
End While
sw1.Stop()

If window IsNot Nothing Then
    DirectCast(window.GetCurrentPattern(WindowPattern.Pattern), WindowPattern).
        SetWindowVisualState(WindowVisualState.Minimized)
End If

暂无
暂无

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

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