如何实现“等待任务”,以便更快地进行Web废料处理。如何使我的代码更快?

时间:2018-11-20 06:05:33

标签: .net vb.net asynchronous async-await

这是基于unix时间戳的数据,需要将其废弃。 服务器中的文件可以是1542688763.png或1542688763.jpg 所以我的系统逐个递减给定的unix时间戳,并检查文件是否存在。 一位Web开发人员做了同样的事情,他的反应很快。他对我说,由于IO绑定和CPU绑定进程使用不当,以及请求和等待代码不正确,我的程序速度较慢。有人可以帮忙吗?

Imports System.Net.Http
Imports System.Net
Imports System.IO
Imports System.Threading



Public Class Form1
    Dim VarHoldingUnix As Double
    Dim KotafOund As Integer
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        VarHoldingUnix = ToUnix(Date.Now)
        TextBox1.Text = VarHoldingUnix
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim DRON As Date
        DRON = FromUnix(VarHoldingUnix, 0)
        MsgBox(DRON)
        MsgBox(DateAndTime.Day(DRON))
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim ABCD As Task


        While Not KotafOund = TextBox3.Text
            ToolStripLabel1.Text = FromUnix(VarHoldingUnix, 0)
            Application.DoEvents()
            ABCD = MakeRequests(300)
        End While

        MsgBox("END")
        End

    End Sub

    Async Function MakeRequests(noS2find As Integer) As Task

        Dim tasks As List(Of Task(Of Boolean)) = New List(Of Task(Of Boolean))
        Dim tasks2 As List(Of Task(Of Boolean)) = New List(Of Task(Of Boolean))
        Dim itemsF As Integer = noS2find

        Dim DRON As Date
        Dim DronDate As String

        Dim MyYear, MyMonth As String

        DRON = FromUnix(VarHoldingUnix, 0)



        For i = 1 To itemsF
            i = i + 1
            DRON = FromUnix(VarHoldingUnix, 0)
            'DronDate = (DateAndTime.Day(DRON))


            Dim MYURLJPG As String = "https://www.secru.com/img/" & VarHoldingUnix & ".png"
            tasks.Add(CheckPageExists1(MYURLJPG))
            'await Task.Run(() => CheckPageExists1(MYURLJPG));
            Dim MYURLPNG As String = "https://www.secru.com/img/" & VarHoldingUnix & ".jpg"
            tasks2.Add(CheckPageExists2(MYURLPNG))
            'await Task.Run(() => CheckPageExists2(MYURLPNG))

            VarHoldingUnix = VarHoldingUnix - 1
        Next

        While tasks.Select(Function(x) x.Result).Count < tasks.Count
            Thread.Sleep(100)
        End While

        While tasks2.Select(Function(x) x.Result).Count < tasks2.Count
            Thread.Sleep(100)
        End While



    End Function

    Private Async Function CheckPageExists1(ByVal url As String) As Task(Of Boolean)


        Dim request As Net.HttpWebRequest = Net.WebRequest.Create(url)
        request.Method = "HEAD"
        'Dim response As Net.HttpWebResponse
        request.Timeout = 5

        Dim Result As Boolean

        Try
            Using response As HttpWebResponse = Await request.GetResponseAsync.ConfigureAwait(False)
                Using responseReader As New IO.StreamReader(response.GetResponseStream)

                    Dim actualResponse As String = Await responseReader.ReadToEndAsync
                    Result = True
                    KotafOund = KotafOund + 1
                    Process.Start(url)
                End Using
            End Using
        Catch ex As Exception
            'IMAGE DOES NOT EXITS
            Result = False
        End Try

        Return Result

    End Function
    Private Async Function CheckPageExists2(ByVal url As String) As Task(Of Boolean)



        Dim request As Net.HttpWebRequest = Net.WebRequest.Create(url)
        'Dim response As Net.HttpWebResponse
        request.Method = "HEAD"
        request.Timeout = 5

        Dim Result As Boolean

        Try
            Using response As HttpWebResponse = Await request.GetResponseAsync.ConfigureAwait(False)
                Using responseReader As New IO.StreamReader(response.GetResponseStream)

                    Dim actualResponse As String = Await responseReader.ReadToEndAsync
                    Result = True
                    KotafOund = KotafOund + 1
                    Process.Start(url)
                End Using
            End Using
        Catch ex As Exception
            'IMAGE DOES NOT EXITS
            Result = False
        End Try

        Return Result

    End Function

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        VarHoldingUnix = TextBox1.Text

        MsgBox("Confirmed New UNIX-VARIABLE " & VarHoldingUnix)
    End Sub
End Class

我认为在异步功能MakeRequests中,我必须使用此代码

While tasks.Select(Function(x) x.Result).Count < tasks.Count
   Await Task.Delay(100)
End While

代替

While tasks.Select(Function(x) x.Result).Count < tasks.Count
   Thread.Sleep(100)
End While

但是我现在无法在等待(100)内设置的毫秒数之间取得完美的平衡,并且

ABCD = MakeRequests(300)

可以是200,也可以是2000。

我不知道。...你们知识渊博

1 个答案:

答案 0 :(得分:-1)

您尝试过吗?

@Path