获取网页调用DownloadStringAsync后()? [英] Getting web page after calling DownloadStringAsync()?

查看:483
本文介绍了获取网页调用DownloadStringAsync后()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道有足够的了解VB.Net尚未使用更丰富的HttpWebRequest类,所以我想我会用简单的WebClient类下载网页异步(避免冻结UI)。

然而,如何能够异步事件处理程序实际上返回网页调用程序?

 进口System.Net公共类Form1的
    私人共享子DownloadStringCallback2(BYVAL发件人为对象,BYVAL E上DownloadStringCompletedEventArgs)
        如果e.Cancelled =假AndAlso运算e.Error是没有那么
            昏暗textString作为字符串= CStr的(e.Result)            在这里:如何textString返回到调用程序?
        万一
    结束小组    私人小组的button1_Click(BYVAL发件人为System.Object的,BYVAL E上System.EventArgs)把手Button1.Click
        昏暗的客户作为Web客户端=新的WebClient()        AddHandler的client.DownloadStringCompleted,AddressOf DownloadStringCallback2
        昏暗URI作为乌里=新的URI(http://www.google.com)        client.DownloadStringAsync(URI)        在这里:如何让网页从回调函数回来?
    结束小组
末级

感谢您。


编辑:我添加了一个全球性的共享变量和一个While /的DoEvents / ENDWHILE,但一定是要做到这一点更清洁的方式: - /

 公共类Form1的
    共享页面作为字符串    公共共享子AlertStringDownloaded(BYVAL发件人为对象,BYVAL E上DownloadStringCompletedEventArgs)
        如果字符串的要求去按计划进行,并没有取消:
        如果e.Cancelled =假AndAlso运算e.Error是没有那么
            页= CStr的(e.Result)
        万一
    结束小组
    私人小组的button1_Click(BYVAL发件人为System.Object的,BYVAL E上System.EventArgs)把手Button1.Click
        昏暗WC作为新的Web客户端        AddHandler的wc.DownloadStringCompleted,AddressOf AlertStringDownloaded        页=什么
        wc.DownloadStringAsync(新的URI(http://www.google.com))
        更好的办法等到页面已经被填补?
        虽然网页是Nothing
            Application.DoEvents()
        虽然结束
        RichTextBox1.Text =页面
    结束小组
末级


解决方案

您可以设置 RichTextBox1.Text 直接在完成处理程序,如果你做出处理函数实例方法,而不是共享的。

 公共类Form1的    私人小组AlertStringDownloaded(BYVAL发件人为对象,BYVAL E上DownloadStringCompletedEventArgs)
        如果字符串的要求去按计划进行,并没有取消:
        如果e.Cancelled =假AndAlso运算e.Error是没有那么
            RichTextBox1.Text = CStr的(e.Result)
        万一
    结束小组
    私人小组的button1_Click(BYVAL发件人为System.Object的,BYVAL E上System.EventArgs)把手Button1.Click
        昏暗WC作为新的Web客户端        AddHandler的wc.DownloadStringCompleted,AddressOf AlertStringDownloaded        页=什么
        wc.DownloadStringAsync(新的URI(http://www.google.com))
    结束小组
末级

I don't know enough about VB.Net yet to use the richer HttpWebRequest class, so I figured I'd use the simpler WebClient class to download web pages asynchronously (to avoid freezing the UI).

However, how can the asynchronous event handler actually return the web page to the calling routine?

Imports System.Net

Public Class Form1
    Private Shared Sub DownloadStringCallback2(ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs)
        If e.Cancelled = False AndAlso e.Error Is Nothing Then
            Dim textString As String = CStr(e.Result)

            'HERE : How to return textString to the calling routine?
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim client As WebClient = New WebClient()

        AddHandler client.DownloadStringCompleted, AddressOf DownloadStringCallback2
        Dim uri As Uri = New Uri("http://www.google.com")

        client.DownloadStringAsync(uri)

        'HERE : how to get web page back from callback function?
    End Sub
End Class

Thank you.


Edit: I added a global, shared variable and a While/DoEvents/EndWhile, but there's got to be a cleaner way to do this :-/

Public Class Form1
    Shared page As String

    Public Shared Sub AlertStringDownloaded(ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs)
        '  If the string request went as planned and wasn't cancelled:
        If e.Cancelled = False AndAlso e.Error Is Nothing Then
            page = CStr(e.Result)
        End If
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim wc As New WebClient

        AddHandler wc.DownloadStringCompleted, AddressOf AlertStringDownloaded

        page = Nothing
        wc.DownloadStringAsync(New Uri("http://www.google.com"))
        'Better way to wait until page has been filled?
        While page Is Nothing
            Application.DoEvents()
        End While
        RichTextBox1.Text = page
    End Sub
End Class

解决方案

You can set the RichTextBox1.Text directly in the completed handler if you make the handler function an instance method instead of shared.

Public Class Form1

    Private Sub AlertStringDownloaded(ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs)
        '  If the string request went as planned and wasn't cancelled:
        If e.Cancelled = False AndAlso e.Error Is Nothing Then
            RichTextBox1.Text = CStr(e.Result)
        End If
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim wc As New WebClient

        AddHandler wc.DownloadStringCompleted, AddressOf AlertStringDownloaded

        page = Nothing
        wc.DownloadStringAsync(New Uri("http://www.google.com"))
    End Sub
End Class

这篇关于获取网页调用DownloadStringAsync后()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆