[UWP]如何在UWP项目中访问其他网站之前等待异步任务完成 [英] [UWP]How to wait for async Task finish before visit other web site in UWP project

查看:247
本文介绍了[UWP]如何在UWP项目中访问其他网站之前等待异步任务完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello:

Hello:

我有一个C#控制台项目,我必须从大约500个URL下载数据并将它们保存到SQL Server数据库中。然后我的另一个UWP项目可以使用数据。

I have a C# console project, which I have to download data from around 500 URLs and save them into SQL Server database. Then my another UWP project can use the data.

现在,我必须合并两个项目中的任务。所以我创建了一个新的UWP项目,但是我可以等待下载工作完成之前我可以在UWP中启动该工作来访问另一个网站吗?

Now, I have to merge the tasks from the both project. So I create a new UWP project, but I how I can wait for the download jobs to finish before I can launch the job in UWP to visit another web site?

以下是我的下载数据作业的代码示例:

The following is code example of how my download data jobs:

列表与LT;串GT; download_urls = new List< string>(){" https:// url1 "," https :// url2 "," https:// url3 ",......" https:// url500 "
};

List<string> download_urls = new List<string>() { "https://url1", "https://url2", "https://url3", …… "https://url500" };

public 静态 async < span style ="color:#000000"> 任务
download_job()

publicstaticasyncTask download_job()

{

HttpClient客户端=
HttpClient();

foreach string url1
< span style ="color:#0000ff"> download_urls)

foreach(stringurl1 indownload_urls)

{

HttpResponseMessage data1 =
等待 client.GetAsync(url1);

Debug.WriteLine( " URL#{0}"
url1);

等待 Task.Delay( 1000);

}

}

注意:1秒的Task.Delay是必需的,否则,下载所有数据会有一些问题。

现在,在我的新UWP项目中,我怎么能把download_job?

我试着把它放在OnNavigatedTo中,如下所示:

受保护 覆盖 async void OnNavigatedTo(NavigationEventArgs
e)

protectedoverrideasyncvoidOnNavigatedTo(NavigationEventArgs e)

{

基础 。OnNavigatedTo(e );

webView1 。LoadCompleted
+ =
webView1 _ LoadCompletedAsync;

webView1 。ScriptNotify
+ =
webView1 _ ScriptNotify;

等待 Task.Delay( 30000);

等待 download_job() ;

}

然而,在运行程序时,它看起来像是download_job( )仅显示前10个下载URL,然后暂停,等待其他
作业运行,如webView1.LoadCompleted;它永远不会完成。

我需要完成所有500个下载工作,然后我就可以了进一步使用webView1.LoadCompleted。

如何在UWP项目中执行此操作?

谢谢,

推荐答案

嗯,一般来说,await操作后的代码不会执行,直到await结束。

Well, generally speaking, codes after the await operation won't execute until the await is finished.

所以我的建议是,你可以在之前调用await download_job()你订阅了webview.loadcompleted事件。

So my suggestion is that, you could call the await download_job() before you subscribe the webview.loadcompleted event.

如果它不起作用,你可以回来看看我们还能做些什么。

If it does not work, you could come back and we shall see what else we can do.

最好的问候,

Roy


这篇关于[UWP]如何在UWP项目中访问其他网站之前等待异步任务完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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