我在VB中有一个程序,它在网站中搜索图像如下:
www.website.com/1.jpg | www.website.com/2.jpg | www.website.com/3.jpg
在循环中,程序在浏览器中只打开3.jpg,因为1和2 jpg不会在服务器中退出。该程序已启动并运行,但速度非常慢,一分钟内大约有120次搜索。然而,我的一位同事在Angular中设计了相同的程序,该程序运行速度非常快,一分钟内搜索量大约为500/600。
他告诉我,他所做的是对服务器产生了50次异步调用,然后又产生了50次,然后又50次并继续这样做。从而使他的程序非常快速和准确。
我研究并了解到,在Visual Basic中,我们有异步和等待调用服务器请求,但我无法弄清楚如何。
这是我现有的代码。任何人都可以帮助我。
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim MYURL as string
itemsF = 0
While Not itemsF = 50000
MYURL = "www.website.com/" & itemsF & ".jpg"
CheckPageExists(MYURL)
itemsF=itemsF+1
End While
End Sub
Private Function CheckPageExists(ByVal url As String) As Boolean
Dim request As Net.HttpWebRequest
Dim response As Net.HttpWebResponse
request = Net.WebRequest.Create(url)
request.Timeout = 5000
Try
response = request.GetResponse()
Catch ex As Exception
'IMAGE DOES NOT EXITS
Exit Function
End Try
Process.Start(url)
End Function
答案 0 :(得分:1)
首先,您必须使CheckPageExists
成为Async
方法:
Private Async Function CheckPageExists(ByVal url As String) As Task(Of Boolean)
Dim request As Net.HttpWebRequest = Net.WebRequest.Create(url)
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
End Using
End Using
Catch ex As Exception
'IMAGE DOES NOT EXITS
Result = False
End Try
Console.WriteLine(url)
''Process.Start("chrome.exe", url)
Return Result
End Function
正如您所看到的,我们使用GetResponse
而不是GetResponseAsync
,而Async
本身就是Return
。这种方法与你之前做的非常相似,我
为了清晰起见,我添加了StreamReader
语句,并在Button2_Click
添加了Async Function MakeRequests() As Task
Dim tasks As List(Of Task(Of Boolean)) = New List(Of Task(Of Boolean))
Dim itemsF As Integer = 5
For i = 1 To itemsF
Dim MYURL As String = "http://www.touchegolfschool.com/images/" & i & ".jpg"
tasks.Add(CheckPageExists(MYURL))
Next
While tasks.Select(Function(x) x.Result).Count < tasks.Count
Thread.Sleep(100)
End While
End Function
来阅读您网站的回复。
完成后,您只需更改tasks.Select(Function(x) x.Result).Count < tasks.Count
方法即可调用其他方法,其中包含您之前所做的所有操作:
Tasks
主要变化是添加// i tried echoing everything possible but no outcome
<?php
include("conn.php");
if(isset($_POST['btn_login'])){
$username = $_POST['username'];
$password = $_POST['password'];
}
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password' LIMIT 1";
$result = mysqli_query($conn,$sql);
$response = array();
if(mysqli_num_rows($result)>0){
$row = mysqli_fetch_row($result);
$username = $row['username'];
$password = $row['password'];
$code = "login_success";
array_push($response,array("code"=>$code,"username"=>$username,"password"=>$password));
echo json_encode($response);
}
else{
$code = "login_failed";
$message = "User not found... Please try again";
array_push($response, array("code"=>$code,"message"=>$message));
echo json_encode($response);
}
mysqli_close($conn);
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="post">
<input type="text" name="username">
<input type="text" name="password">
<input type="submit" name="btn_login">
</form>
</body>
</html>
;你之前看到的是一个请求,但由于返回的时间从未返回;告诉主函数等待所有请求都有结果,使应用程序等待足够长的时间来响应。
这里的主要区别在于使用{{1}}。
检查this以获取有关异步编程的官方文档。