将C#lambda表达式转换为vb.net
我在C#中有三个语句,在将它们转换为vb.net时遇到问题,有人能告诉我如何在vb.net中实现这一点吗将C#lambda表达式转换为vb.net,c#,vb.net,C#,Vb.net,我在C#中有三个语句,在将它们转换为vb.net时遇到问题,有人能告诉我如何在vb.net中实现这一点吗 _hubConnection.Reconnecting += () => { if (_hubConnection.State == ConnectionState.Reconnecting) { CanSend = false; Status = "Connection reconnecting..."; } }; _hubC
_hubConnection.Reconnecting += () =>
{
if (_hubConnection.State == ConnectionState.Reconnecting)
{
CanSend = false;
Status = "Connection reconnecting...";
}
};
_hubConnection.Closed += async () =>
{
if (_hubConnection.State == ConnectionState.Disconnected)
{
CanSend = false;
Status = "Connection lost, reconnecting in a bit...";
await Task.Delay(_reconnectDelay);
await Connect();
}
};
_hubConnection.Error += ex =>
{
LogMessages.Add(ex.ToString());
};
到目前为止我做了什么(请确认是否可以,最后一个我不知道:
AddHandler\u hubConnection.Reconnecting,Sub()
如果_hubConnection.State=ConnectionState.Connected,则
CanSend=false;
状态=“连接重新连接…”结束子节点)
如果结束
端接头
第二条:
AddHandler\u hubConnection.Closed,Async Sub()
如果_hubConnection.State=ConnectionState.Disconnected,则
CanSend=false;
Status=“连接丢失,稍后重新连接…”;
等待任务延迟(重新连接延迟);
等待连接();
如果结束
端接头
附加问题:
Private thisLock As New Object
Private Const _hubUrl As String = "http://localhost:4848"
Private _hubConnection As HubConnection
Private _hubProxy As IHubProxy
Private _connectionStateLocker As New Object()
Private _canSend As Boolean
Private _status As String
Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Await Connect()
Catch ex As Exception
SyncLock thisLock
msg_listview.Items.Add(New ListBoxItem With {.BackColors = New Color() {Color.Red}, .TextColor = Color.White, .Text = "Form1_Load: " + ex.ToString()})
End SyncLock
End Try
End Sub
Async Function Connect() As Task
SyncLock _connectionStateLocker
'this is protect against connecting for only once..
If _hubConnection IsNot Nothing AndAlso _hubConnection.State <> ConnectionState.Disconnected Then
Return
End If
_hubConnection = New HubConnection(_hubUrl)
AddHandler _hubConnection.Reconnecting, Sub()
If _hubConnection.State = ConnectionState.Connected Then
_canSend = False
msg_listview.Invoke(Sub()
msg_listview.Items.Add(New ListBoxItem With {.BackColors = New Color() {Color.Black}, .TextColor = Color.White, .Text = "Connection reconnecting"})
End Sub)
End If
End Sub
AddHandler _hubConnection.StateChanged, Sub(e)
If e.OldState = ConnectionState.Reconnecting AndAlso e.NewState = ConnectionState.Connected Then
msg_listview.Invoke(Sub()
msg_listview.Items.Add(New ListBoxItem With {.BackColors = New Color() {Color.Black}, .TextColor = Color.White, .Text = String.Format("Connected to {0} via {1}", _hubUrl, _hubConnection.Transport.Name)})
End Sub)
_canSend = True
End If
End Sub
AddHandler _hubConnection.Closed, Async Sub()
_canSend = False
msg_listview.Invoke(Sub()
msg_listview.Items.Add(New ListBoxItem With {.BackColors = New Color() {Color.Black}, .TextColor = Color.White, .Text = "Connection lost, reconnecting in a bit..."})
End Sub)
_canSend = True
Await Task.Delay(3000)
Await Connect()
End Sub
AddHandler _hubConnection.Error, Sub(ex)
msg_listview.Invoke(Sub()
msg_listview.Items.Add(New ListBoxItem With {.BackColors = New Color() {Color.Black}, .TextColor = Color.White, .Text = "ERROR:" + ex.ToString})
End Sub)
End Sub
_hubProxy = _hubConnection.CreateHubProxy("Main")
_hubProxy.[On](Of String)("heartbeat", (Sub()
msg_listview.Invoke(Sub()
msg_listview.Items.Add("Recieved hearbeat")
End Sub)
End Sub))
_hubProxy.[On](Of HelloModel)("sendHelloObject", Sub(hello)
msg_listview.Invoke(Sub()
msg_listview.Items.Add("Recieved sendHelloObject: Molly: " + hello.Molly + " Age: " + hello.Age.ToString())
End Sub)
End Sub)
msg_listview.Invoke(Sub()
msg_listview.Items.Add("Connecting...")
End Sub)
End SyncLock
'Keep trying to connect until it works (dont just call Start expect to work as one time server could not be available)
'so what we gonna to do is retry the loop over and over again until such time it works.
While True
Try
Await _hubConnection.Start
msg_listview.Invoke(Sub()
msg_listview.Invoke(Sub()
msg_listview.Items.Add(New ListBoxItem With {.BackColors = New Color() {Color.Black}, .TextColor = Color.White, .Text = String.Format("Connected to {0} via {1}", _hubUrl, _hubConnection.Transport.Name)})
End Sub)
End Sub)
_canSend = True
Exit While
Catch ex As Exception
End Try
End While
End Function
Private thisLock作为新对象
Private Const_hubUrl作为字符串=”http://localhost:4848"
专用_hubConnection作为hubConnection
Private\u hubbroxy作为ihubbroxy
Private _connectionStateLocker作为新对象()
Private\u可以作为布尔值发送
私有\u状态为字符串
私有异步子表单1_Load(发送方作为对象,e作为事件参数)处理MyBase.Load
尝试
等待连接()
特例
同步锁定此锁定
msg_listview.Items.Add(带有{.BackColors=New Color(){Color.Red}、.TextColor=Color.White、.Text=“Form1_Load:”+ex.ToString()}的新ListBoxItem)
端同步
结束尝试
端接头
异步函数Connect()作为任务
同步锁\u连接状态锁
'这是为了防止仅连接一次。。
如果_hubConnection不是空的,并且_hubConnection.State ConnectionState.Disconnected也不是空的,那么
返回
如果结束
_hubConnection=新的hubConnection(\u hubUrl)
AddHandler\u hubConnection.Reconnecting,Sub()
如果_hubConnection.State=ConnectionState.Connected,则
_canSend=False
msg_listview.Invoke(Sub()
msg_listview.Items.Add(带有{.BackColors=New Color(){Color.Black}、.TextColor=Color.White、.Text=“连接重新连接”}的新ListBoxItem)
末端接头)
如果结束
端接头
AddHandler\u hubConnection.StateChanged,Sub(e)
如果e.OldState=ConnectionState.Reconnecting,并且e.NewState=ConnectionState.Connected,则
msg_listview.Invoke(Sub()
msg_listview.Items.Add(带有{.BackColors=New Color(){Color.Black}、.TextColor=Color.White、.Text=String.Format的新ListBoxItem(“通过{1}连接到{0},_hubUrl,_hubConnection.Transport.Name)})
末端接头)
_canSend=True
如果结束
端接头
AddHandler _hubConnection.Closed,异步子()
_canSend=False
msg_listview.Invoke(Sub()
msg_listview.Items.Add(带有{.BackColors=New Color(){Color.Black}、.TextColor=Color.White、.Text=“连接丢失,稍后重新连接…”的新ListBoxItem)
末端接头)
_canSend=True
等待任务。延迟(3000)
等待连接()
端接头
AddHandler\u hubConnection.Error,Sub(ex)
msg_listview.Invoke(Sub()
msg_listview.Items.Add(带有{.BackColors=New Color(){Color.Black}、.TextColor=Color.White、.Text=“ERROR:+ex.ToString}的新ListBoxItem)
末端接头)
端接头
_hubProxy=\u hubConnection.CreateHubProxy(“主”)
_hubProxy.[On](指字符串)(“heartbeat”,(Sub()
msg_listview.Invoke(Sub()
msg_listview.Items.Add(“received hearbeat”)
末端接头)
末端接头)
_hubProxy.[On](HelloModel的)(“sendHelloObject”,Sub(您好)
msg_listview.Invoke(Sub()
msg_listview.Items.Add(“接收到的sendHelloObject:Molly:+hello.Molly+”Age:+hello.Age.ToString())
末端接头)
末端接头)
msg_listview.Invoke(Sub()