簡體   English   中英

將C#lambda表達式轉換為vb.net

[英]Convert C# lambda expression to vb.net

我在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());
};

我到目前為止所做的事情(請確認是否還可以,最后一個我不知道:

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

編輯:

    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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM