有一些mdi应用程序。在该应用程序中,用户可以打开某个表单来完成一些工作。他可以打开多种形式。每次他打开一个新的,他要求在小形式之前提供IP地址,哪个值传递给表单。在每个表单中,我正在尝试实现某些引擎,以便不时给出ip,并在状态栏控件上显示它的状态。我决定使用async/await
,但是我有问题,当用户输入IP地址并单击确定按钮,然后出现而不是FrmDrukujEtykiete,我的主应用程序类中出现错误。你能支持我如何使它有效吗?
这是出现错误的地方:
Public Sub main()
Application.EnableVisualStyles()
Dim result As DialogResult
Using frmL As New FrmLogin
result = frmL.ShowDialog
End Using
If result = DialogResult.OK Then
Dim FrmMain As New FrmMain()
Application.Run(FrmMain)
End If
End Sub
End Module
此行显示错误:
Application.Run(FrmMain)
那句话:
Additional information: Exception has been thrown by the target of an invocation.
所以一步一步:用户点击mdi格式的按钮somwhere然后有这个代码:
Dim pobierzIP As New FrmPobierzIP 'asking form to get ip
If pobierzIP.ShowDialog() = DialogResult.OK Then
Dim drukujEtykiete As New FrmDrukujEtykiete(pobierzIP.Ip) 'targeted form
....
这就是我正在尝试为我的ping实现async / await的地方 - 这里的错误是错误的:
Public Class FrmDrukujEtykiete
Private Property IPAddress As String
Sub New(ipaddress As String)
InitializeComponent()
PingPong()
Me.IPAddress = ipaddress
End Sub
Private Sub btnWyjdz_Click(sender As Object, e As EventArgs) Handles btnWyjdz.Click
Close()
End Sub
Private Async Sub PingPong()
While (True)
Dim mytask As Task(Of Boolean)
mytask = Task.Factory.StartNew(IsDestinationReachable(IPAddress))
If Await mytask Then
tsPingResultIcon.BackColor = Color.Green
tsPingResultIcon.Text = "Reachable"
Else
tsPingResultIcon.BackColor = Color.Red
tsPingResultIcon.Text = "Not reachable"
End If
End While
End Sub
Public Function IsDestinationReachable(ByVal hostnameOrAddress As String)
Dim reachable As Boolean = False
Try
reachable = My.Computer.Network.IsAvailable AndAlso
My.Computer.Network.Ping(hostnameOrAddress)
Catch pingException As System.Net.NetworkInformation.PingException
Catch genericNetworkException As System.Net.NetworkInformation.NetworkInformationException
' Fail silently and return false
End Try
Return reachable
End Function
End Class
编辑:根据建议我在Application.Run(FrmMain)中尝试捕获 这是我得到的错误:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentNullException: Value cannot be null.
Parameter name: hostNameOrAddress
at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer)
at Microsoft.VisualBasic.Devices.Network.Ping(String hostNameOrAddress, Int32 timeout)
at Microsoft.VisualBasic.Devices.Network.Ping(String hostNameOrAddress)
at FrmDrukujEtykiete.IsDestinationReachable(String hostnameOrAddress) in C:\Pliki\_Projekty\Tompson\Tompson\Tompson\FrmDrukujEtykiete.vb:line 37
at FrmDrukujEtykiete.VB$StateMachine_70_PingPong.MoveNext() in C:\Pliki\_Projekty\Tompson\Tompson\Tompson\FrmDrukujEtykiete.vb:line 21
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_0(Object state)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
EDIT3:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidCastException: Unable to cast object of type 'System.Boolean' to type 'System.Action'.
at FrmDrukujEtykiete.VB$StateMachine_70_PingPong.MoveNext() in C:\Pliki\_Projekty\sol\FrmDrukujEtykiete.vb:line 22
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_0(Object state)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
答案 0 :(得分:1)
错误表明调用hostNameOrAddress
时System.Net.NetworkInformation.Ping.Send
不能为空。
这使人们相信它没有收到传递给它的价值。
您的PingPong()
方法正在使用私有属性IPAddress来了解要ping的内容。但是,构造函数在为该属性赋值之前调用PingPong()
,因此当PingPong()
调用.Ping()
时,IPAddress字段为空。
Sub New(ipaddress As String)
InitializeComponent()
PingPong()
Me.IPAddress = ipaddress
End Sub
交换构造函数的第二行和第三行,在调用PingPong()
之前将值赋给属性。