如何在ASP:NET / C#中等待一些任务完成 [英] How to wait for some task to finish in ASP:NET/C#

查看:120
本文介绍了如何在ASP:NET / C#中等待一些任务完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

受保护的 void  Page_Load( object  sender,EventArgs e)
{
XDocument response = ThirdPartyAPI.HttpRequest(someParams);

if (response!= null
{
var node = response.Root.Elements( 文档)。元素( document);
string url = node.ElementAt( 0 )。元素( url)。值;


UploadFileToXYZSiteWithHttpRequest(token,parentID,url);
}
}





我的方案如下:

我的问题在这里是当我的项目运行时,响应变量 null

但是当我调试并在响应变量之前放置一个断点并等待几秒钟时,让我们说3秒,我得到了我期待的响应。



似乎我必须等待几秒钟才能执行HttpRequest()函数以便有响应。 />


我尝试在提到的代码行之前放置Thread.Sleep(3000)方法,但现在我的第二个函数( UploadFileToXYZSiteWithHttpRequest )它没有做它的工作。



我应该等待一些事情要完成(我不知道是什么,可能是页面加载,它在后台的一个线程)然后执行ThirdPartyAPI.HttpRequest方法?



我应该使用Threads还是Task< tresult>上课还是其他什么?如果是这样,怎么做到这一点?



请帮我澄清一下解决这个问题的方法。

谢谢。

解决方案

在我看来,ThirdPartyAPI.HttpRequest是异步方法,在这种情况下,ThirdPartyAPI中应该有一些方法或属性来指示HttpRequest是否仍然处于忙碌或完成状态。可以使用API​​代替

  if (response!= null)





你能看一下吗?


你可以使用Async和Await关键字,如果它是C#4.5或以上......

查看此内容

< img src =https://i-msdn.sec.s-msft.com/dynimg/IC612215.png/>

https://i-msdn.sec.s-msft.com/dynimg/ IC612215.png [ ^ ]



谢谢

Protected void Page_Load(object sender, EventArgs e)
{
    XDocument response = ThirdPartyAPI.HttpRequest(someParams);

    if(response != null)
    {
       var node = response.Root.Elements("documents").Elements("document");
       string url = node.ElementAt(0).Element("url").Value;

       
        UploadFileToXYZSiteWithHttpRequest(token, parentID,url);
    }
}



My scenario as following:
My problem here is that the response variable is null when my project is running.
But when I am debugging and put a breakpoint before the response variable and wait for some few seconds, lets say 3 seconds, I get the response I am expecting.

It seems that I have to wait for some seconds before executing HttpRequest() function in order have a response.

I tried putting the Thread.Sleep(3000) method before the mentioned code line but now my second function (UploadFileToXYZSiteWithHttpRequest) its not doing its work.

Should I wait for something to finish (I don't know what, may be the page load, a thread that its in the background) for then to execute the ThirdPartyAPI.HttpRequest method?

should I use Threads or Task<tresult> class or anything else? if so, how to achieve this?

Please help me to clarify my way to solve this.
thanks.

解决方案

Looks to me that ThirdPartyAPI.HttpRequest is asynchronous method, in which case there should be some method or property in ThirdPartyAPI that indicates whether the HttpRequest is still busy or done..and that very API could be used in lieu of

if(response != null)



Can you look into that?


You can use "Async" and "Await" keywords ,if it's C# 4.5 or above..
Check this out
<img src="https://i-msdn.sec.s-msft.com/dynimg/IC612215.png"/>
https://i-msdn.sec.s-msft.com/dynimg/IC612215.png[^]

Thanks


这篇关于如何在ASP:NET / C#中等待一些任务完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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