Reputation: 132
public void RemoteInsert()
{
String dataSet = "dataset=" + json.ToString();
accessKey = mobRemoteDB.accessKey;
String paramaters = "?llcommand=sndmsg&" + "ak=" + accessKey + "&command=remoteinsert&" + dataSet;
String url = mobRemoteDB.baseUrlForRemoteInsert + paramaters;
HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/json";
if (NetworkInterface.GetIsNetworkAvailable() & IsDataValid)
{
webRequest.BeginGetRequestStream(newAsyncCallback(GetRequestStreamCallback),webR equest);
}
}
void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
}
void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamResponse);
JObject json_response = JObject.Parse(streamReader.ReadToEnd());
String key = (String)json_response["ret"];
JObject dic = (JObject)json_response["retdic"];
}
catch(WebException e)
{
}
}
I want RemoteInsert() method to wait for my callback GetResponseCallback.
I am Unable to do that as tried many things.
Can any body Please Assist me here.
I do not want the main thread to work something else until this response comes .
Please Suggest. as after that response i have to use it for next execution.
Here My code is going like
1. Task(A).running----await
2. ------------------------------->Task(B)
3 ---------Response From Task(A)------->Task(C)
I want to Implement it like First Task A Completes , Then Task(C) as it is on Success of Task(A). Then Task(B)
Upvotes: 0
Views: 6167
Reputation: 1683
If I understand your question, you're wanting to perform the webrequest on the calling thread, and wait for it to complete before continuing?
You can simplify your RemoteInsert()
method by using async/await, and using the GetResponseAsync()
method to fetch the response object in one call. Something like this:
public async Task RemoteInsert()
{
String dataSet = "dataset=" + json.ToString();
accessKey = mobRemoteDB.accessKey;
String paramaters = "?llcommand=sndmsg&" + "ak=" + accessKey + "&command=remoteinsert&" + dataSet;
String url = mobRemoteDB.baseUrlForRemoteInsert + paramaters;
HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/json";
if (NetworkInterface.GetIsNetworkAvailable() && IsDataValid)
{
// Await the GetResponseAsync() call.
using(var response = await webRequest.GetResponseAsync().ConfigureAwait(false))
{
// The following code will execute on the main thread.
using (var streamResponse = response.GetResponseStream())
{
using (StreamReader streamReader = new StreamReader(streamResponse))
{
JObject json_response = JObject.Parse(streamReader.ReadToEnd());
String key = (String)json_response["ret"];
JObject dic = (JObject)json_response["retdic"];
}
}
}
}
}
And lose the other two methods, as they're no longer required.
When you call this method, you can Wait() on it:
RemoteInsert().Wait();
However, WP tries to enforce good async patterns to avoid doing this. What is your need to block the UI thread in this case?
In your method that is calling RemoteInsert() ... lets assume it's some kind of button-click event ... can you not do this?
public async void Button_Click()
{
// First call Remote insert, and await the result.
// The web-request will not block the UI thread.
await RemoteInsert();
// This code will be executed after RemoteInsert() completes:
... perform additional tasks that are dependant on RemoteInsert()
}
In other words, rather than blocking the UI thread - you execute those other methods AFTER the RemoteInsert() method has completed.
Upvotes: 3