将c# lambda表达式转换为vb.net
本文关键字:vb net 转换 表达式 lambda | 更新日期: 2023-09-27 18:15:25
我在c#中有三个语句,并且有问题将它们转换为vb.net,谁能展示如何在vb.net中完成?:
_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());
};
到目前为止我做了什么(请确认是否ok,最后一个我不知道:
AddHandler _hubConnection.Reconnecting, Sub()
If _hubConnection.State = ConnectionState.Connected Then
CanSend = false;
Status = "Connection reconnecting..." End Sub)
End If
End Sub
第二个:
AddHandler _hubConnection.Closed, Async Sub()
If _hubConnection.State = ConnectionState.Disconnected Then
CanSend = false;
Status = "Connection lost, reconnecting in a bit...";
await Task.Delay(_reconnectDelay);
await Connect();
End If
End Sub
附加问题:
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
edit:
AddHandler _hubConnection.Reconnecting, Sub()
If _hubConnection.State = ConnectionState.Reconnecting Then
CanSend = False
Status = "Connection reconnecting..."
End If
End Sub
AddHandler _hubConnection.Reconnected, Sub()
If _hubConnection.State = ConnectionState.Connected Then
Status = String.Format("Connected to {0} via {1}", _hubUrl, _hubConnection.Transport.Name)
CanSend = True
End If
End Sub
AddHandler _hubConnection.Closed, Async Sub()
If _hubConnection.State = ConnectionState.Disconnected Then
CanSend = False
Status = "Connection lost, reconnecting in a bit..."
Await Task.Delay(_reconnectDelay)
Await Connect()
End If
End Sub
AddHandler _hubConnection.Error, Sub(ex)
LogMessages.Add(ex.ToString())
End Sub