dlgDisplayMessage
关于表单处理命令行参数的方式的一些建议:
您应该使用Environment.GetCommandLineArgs()来获取传递给命令行的值数组。这些值是用空格隔开的。
第一项始终表示可执行路径。推荐,这是一种 .Net 方法,更容易翻译成另一种 .Net 语言。
您也可以使用My.Application.CommandLineArgs,不同之处在于第一项是命令行的第一个参数,而不是可执行路径。避免Interaction.Command()
我认为那些lblMessageLine01
等部分实际上是一些标签的内容。在这种情况下,您当然应该使用该lblMessageLine01.Text
属性。您可以使用内插字符串将这些值添加到命令行。由于这些标签可能包含由空格分隔的多个单词,因此您需要将这些值括在双引号中。例如:
Dim commandLine = $"FormName C ""{lblMessageLine01.Text}${lblMessageLine02.Text}"""
要使用 Process 类从 Dialog 返回值,您有几个选项:
当然,您也可以使用某种形式的进程间通信,但这与 OP 中描述的情况不同。
Imports System.IO
Public Class dlgDisplayMessage
' For testing. Replace the Bitmaps with anything else
Private images As New Dictionary(Of String, Bitmap) From {
{"C", SystemIcons.Warning.ToBitmap()},
{"E", SystemIcons.Exclamation.ToBitmap()},
{"Q", SystemIcons.Question.ToBitmap()}
}
Protected Overrides Sub OnLoad(e As EventArgs)
' The first item is always the executable path
Dim arParameter = Environment.GetCommandLineArgs()
If arParameter.Count > 1 AndAlso images.ContainsKey(arParameter(1)) Then
pbPictureBox.Image = images(arParameter(1))
End If
If arParameter.Count > 2 Then
txtMessageBody.Visible = True
txtMessageBody.AppendText(String.Join(Environment.NewLine, arParameter(3).Split("$"c)))
End If
MyBase.OnLoad(e)
End Sub
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
Console.WriteLine("Some data to return")
Console.Out.WriteLine("More data")
Environment.Exit(DialogResult.OK)
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Using sw = New StreamWriter(Console.OpenStandardOutput())
sw.AutoFlush = True
sw.WriteLine("Other data to return")
End Using
Environment.Exit(DialogResult.Cancel)
End Sub
Private Sub btnAbort_Click(sender As Object, e As EventArgs) Handles btnAbort.Click
Console.Error.WriteLine("Some errors to report")
Environment.Exit(DialogResult.Abort)
End Sub
End Class
启动这个可执行文件的应用程序可以通过不同的方式获得对话框的结果:读取它启动的进程的 StandardOutput、StandardError 或 ExitCode。或者全部,当然。
我假设创建这个对话框的应用程序在你的控制之下(你做了它)。
在任何情况下,Process StartInfo必须将RedirectStandardOutput设置为True
,可选择将 RedirectStandardError设置为True
并将UseShellExecute 设置为False
(从 System Shell 开始,此处不允许重定向)
然后您可以:
启动进程
阅读 StandardOutput 和 StandardError
使用Process.WaitForExit()同步等待进程退出
阅读流程ExitCode
,例如:
[Process].Start()
Dim sOut = [Process].StandardOutput.ReadToEnd()
Dim sErr = [Process].StandardError.ReadToEnd()
[Process].WaitForExit()
Dim exitCode = [Process]?.ExitCode
[Process]?.Dispose()
这个过程是同步的(阻塞的)。当进程启动并且它的结果在 GUI 中等待时,您很有可能不希望这样做,因为它也会阻塞用户界面。
当然,您可以运行任务或启动线程,然后将结果编组回 UI 线程。
您还可以使用异步(事件驱动)版本,订阅OutputDataReceived、ErrorDataReceived和Exited事件。
要启用该Exited
事件,您需要将Process.EnableRaisingEvents设置为True
。还将Process.SynchronizingObject
设置为将处理事件的 Control 类(通常是 Form,但任何对象都可以)的实例。这是因为 Process 的事件是在 ThreadPool 线程中引发的。将 UI 元素设置为,会导致事件在创建指定对象的同一线程中引发(此处为 UI 线程)。ISynchronizeInvoke
SynchronizingObject
这在现有上下文中可能有点令人讨厌,因为您必须将事件处理程序添加到 Form 类,记住删除它们,处理 Processasynchronously
等。
所以这里有一个帮助类,它可以将事件驱动的过程转换为一个可以从任何异步方法执行的等待任务。例如,它可以从Click
Button 的事件处理程序中调用,将Async
关键字添加到方法中。
它可以被修改并用于测试不同的场景和方法来启动一个流程并在一个单独的环境中获得它的结果。
它使用TaskCompletionSource + Task.WhenAny()。
该Exited
事件导致TaskCompletionSource
设置其结果。
帮助类返回 Process 的 StandardOutput、StandardError 和 ExitCode 值转换为DialogResult值的内容。
如果指定,它可以设置一个超时,以停止等待进程返回结果。
示例调用程序:
Private Async Sub SomeButton_Click(sender As Object, e As EventArgs) Handles SomeButton.Click
Dim exePath = "[The Executable Path]"
Dim cmdLine = $"FormName C ""{lblMessageLine01.Text}${lblMessageLine02.Text}${lblMessageLine03.Text}"""
Dim dlgResponse = New DialogResponseHelper(exePath, cmdLine)
' This call returns when the Dialog is closed or a Timeout occurs
Dim exitCode = Await dlgResponse.Start()
' Read the StandardOutput results
If dlgResponse.DialogData.Count > 0 Then
For Each dataItem In dlgResponse.DialogData
Console.WriteLine(dataItem)
Next
End If
' See whether errors are signaled
Console.WriteLine(dlgResponse.DialogErrors.Count)
If dlgResponse.ExitCode = DialogResult.OK Then
' Do something
End If
End Sub
如果插值字符串功能不可用,请String.Format()
改用:
Dim dataOut = {lblMessageLine01.Text, lblMessageLine02.Text, lblMessageLine03.Text}
Dim cmdLine = String.Format("FormName C ""{0}${1}${2}""", dataOut)
DialogResponseHelper
班级:
Imports System.Collections.Concurrent
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.IO
Imports System.Linq
Imports System.Threading
Imports System.Threading.Tasks
Imports System.Windows.Forms
Public Class DialogResponseHelper
Private exitedTcs As TaskCompletionSource(Of Integer) = Nothing
Private dlgData As New ConcurrentBag(Of String)()
Private dlgErrors As New ConcurrentBag(Of String)()
Private mExitCode As DialogResult = DialogResult.None
Private proc As Process = Nothing
Public Sub New(exePath As String, cmdLine As String, Optional timeout As Integer = Timeout.Infinite)
ExecutablePath = exePath
CommandLine = cmdLine
WaitTimeout = timeout
End Sub
Public ReadOnly Property ExecutablePath As String
Public ReadOnly Property CommandLine As String
Public ReadOnly Property WaitTimeout As Integer
Public ReadOnly Property ExitCode As DialogResult
Get
Return mExitCode
End Get
End Property
Public ReadOnly Property DialogData As List(Of String)
Get
Return dlgData.ToList()
End Get
End Property
Public ReadOnly Property DialogErrors As List(Of String)
Get
Return dlgErrors.ToList()
End Get
End Property
Public Async Function Start() As Task(Of Integer)
exitedTcs = New TaskCompletionSource(Of Integer)()
proc = New Process()
Dim psi = New ProcessStartInfo(ExecutablePath, CommandLine) With {
.RedirectStandardError = True,
.RedirectStandardOutput = True,
.UseShellExecute = False,
.WorkingDirectory = Path.GetDirectoryName(ExecutablePath)
}
proc.StartInfo = psi
AddHandler proc.OutputDataReceived, AddressOf DataReceived
AddHandler proc.ErrorDataReceived, AddressOf ErrorReceived
AddHandler proc.Exited, AddressOf ProcessExited
proc.EnableRaisingEvents = True
proc.Start()
proc.BeginErrorReadLine()
proc.BeginOutputReadLine()
Await Task.WhenAny(exitedTcs.Task, Task.Delay(WaitTimeout))
If proc IsNot Nothing Then
RemoveHandler proc.Exited, AddressOf ProcessExited
RemoveHandler proc.ErrorDataReceived, AddressOf ErrorReceived
RemoveHandler proc.OutputDataReceived, AddressOf DataReceived
proc.Dispose()
proc = Nothing
End If
Return exitedTcs.Task.Result
End Function
Private Sub DataReceived(sender As Object, e As DataReceivedEventArgs)
If String.IsNullOrEmpty(e.Data) Then Return
dlgData.Add(e.Data)
End Sub
Private Sub ErrorReceived(sender As Object, e As DataReceivedEventArgs)
If String.IsNullOrEmpty(e.Data) Then Return
dlgErrors.Add(e.Data)
End Sub
Private Sub ProcessExited(sender As Object, e As EventArgs)
Dim exitId = (proc?.ExitCode).Value
mExitCode = CType(exitId, DialogResult)
exitedTcs.TrySetResult(exitId)
End Sub
End Class