Click here to Skip to main content
15,884,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
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.
Posted

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
VB
if(response != null)


Can you look into that?
 
Share this answer
 
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
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900