vb.net 如何在VB.NET后台运行函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7868623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:45:09  来源:igfitidea点击:

How to run function in background in VB.NET?

vb.netbackground-process

提问by SharkTheDark

I am developing app in VB.NET that should have HTML requests and getting responses as string.

我正在 VB.NET 中开发应用程序,该应用程序应该具有 HTML 请求并以字符串形式获取响应。

Now I am using this code:

现在我正在使用此代码:

    Dim URL as String = "http://domain.com/page.php"
    Dim webClient As System.Net.WebClient = New System.Net.WebClient()
    Dim result As String = webClient.DownloadString(URLL)

But when I run this code, my program froze for a little bit, until this URL content is loaded.

但是当我运行这段代码时,我的程序冻结了一点,直到加载了这个 URL 内容。

I searched over google and I didn't find anything about this. I tried using Classes and Modules, but with both of those it's frizzing again.

我在谷歌上搜索,我没有找到任何关于这个的信息。我尝试使用类和模块,但使用这两个它再次毛躁。

Can someone explain me how to run this in background, so program won't froze, and when it get's downloaded to return data?

有人可以解释我如何在后台运行它,这样程序就不会冻结,并且何时下载以返回数据?

Thanks.

谢谢。

回答by Tim

I believe what you're looking for is the BackgroundWorker Class. This will allow you to spin the request off on a separate thread without freezing the UI. It's a pretty common thing.

我相信您正在寻找的是BackgroundWorker Class。这将允许您在单独的线程上关闭请求,而不会冻结 UI。这是很常见的事情。

Here's a couple of examples:

下面是几个例子:

VB.NET BackgroundWorker Use

VB.NET BackgroundWorker 使用

BackgroundWorker Demo

后台工作演示