发射订单问题

时间:2018-02-22 16:33:20

标签: c# .net mvp

我遇到一个问题,其中有一行代码在它应该执行之前(至少在我看来)。有问题的代码是下面按钮点击事件中代码的第3行。我无法弄清楚为什么会这样。

代码的第2行将事件发送到关联的Presenter,在演示者事件中的代码执行完之前,第3行被触发。

也许我错过了一些明显的东西,却想不出来。

private async void _mainView_GetDataButtonClick(object sender, EventArgs e)
{
}

我在这里缺少什么?

修改

GetDataButtonClick的定义是:

Sub scrape()


    Dim appIE As Object
    Dim ihtml As Object
    Dim ThisPage As Long
    Dim TotalPages As Long
    Dim allRowOfData As Object
    Dim r As Long
    Dim curHTMLRow As Object

    ThisPage = 1
    Set appIE = CreateObject("internetexplorer.application")

    With appIE

        .Visible = False
        .navigate "http://www.boliga.dk/salg/resultater?so=1&sort=omregnings_dato-d&maxsaledate=today&iPostnr=&gade=&type=Villa&minsaledate=2017&p=" & ThisPage

        While .Busy = True Or .readyState < 4: DoEvents: Wend

        Set ihtml = .document
        'we get total result number to calculate how many pages
        For Each allRowOfData In appIE.document.getElementsByTagName("h4")
            If Right(allRowOfData.innerText, 10) = "resultater" Then

                TotalPages = Application.WorksheetFunction.RoundUp(onlyDigits(allRowOfData.innerText) / 40, 0)
                Exit For
            End If
        Next

        For ThisPage = 1 To TotalPages Step 1


            .navigate "http://www.boliga.dk/salg/resultater?so=1&sort=omregnings_dato-d&maxsaledate=today&iPostnr=&gade=&type=Villa&minsaledate=2017&p=" & ThisPage
            While .Busy = True Or .readyState < 4: DoEvents: Wend

            Set allRowOfData = Nothing

            Set allRowOfData = appIE.document.getElementById("searchresult")

            For r = 1 To allRowOfData.Rows.Length - 1

                Set curHTMLRow = allRowOfData.Rows(r)

                If ThisPage = 1 Then
                    Cells(r + 1, 1) = curHTMLRow.Cells(7).innerText
                Else
                    Cells(Range("A2").End(xlDown).Row + 1, 1) = curHTMLRow.Cells(7).innerText
                End If

            Next r
        Next ThisPage

        .Quit

    End With

    Set appIE = Nothing


MsgBox "Scrapping done"
End Sub

Private Function onlyDigits(s As String) As String
    ' Variables needed (remember to use "option explicit").   '
    Dim retval As String    ' This is the return string.      '
    Dim i As Integer        ' Counter for character position. '

    ' Initialise return string to empty                       '
    retval = ""

    ' For every character in input string, copy digits to     '
    '   return string.                                        '
    For i = 1 To Len(s)
        If Mid(s, i, 1) >= "0" And Mid(s, i, 1) <= "9" Then
            retval = retval + Mid(s, i, 1)
        End If
    Next

    ' Then return the return string.                          '
    onlyDigits = retval
End Function

1 个答案:

答案 0 :(得分:0)

在评论中,您说GetDataButtonClick是一种async Task方法。在这种情况下:

  1. GetDataButtonClick重命名为GetDataButtonClickAsync,因为在异步工作并以Task表示其返回值的.NET方法应该具有方法名称后缀Async让人们清楚地看到你的代码是一种非阻塞的TPL方法。

  2. btnGetData_Click更改为async void方法。请注意,事件处理程序(通常)仅 时间可以接受async void而不是async Task。这也意味着你不应该直接调用btnGetData_Click:因为调用者没有关于方法最终完成的信息(而且你不能使用{{ 1}}因为async Task要求返回类型为System.EventHandler

  3. 添加void,但不添加await,因为.ConfigureAwait(false)完成后应在UI线程上恢复执行。

  4. 像这样:

    GetDataButtonClick