After searching in the Net for about 4 hours I still don't understand Async functions on Windows Phone 7. I tried to run that code but it looks like the event "DownloadStringCompleted" for my webClient is never raised. I tried to wait here for an answer, but it just freeze my app. Anyone could help and explain why it don't work?
internal string HTTPGet()
{
string data = null;
bool exit = false;
WebClient webClient = new WebClient();
webClient.UseDefaultCredentials = true;
webClient.DownloadStringCompleted += (sender, e) =>
{
if (e.Error == null)
{
data = e.Result;
exit = true;
}
};
webClient.DownloadStringAsync(new Uri(site, UriKind.Absolute));
//while (!exit)
// Thread.Sleep(1000);
return data;
}
Ok. Found something!
http://blogs.msdn.com/b/kevinash/archive/2012/02/21/async-ctp-task-based-asynchronous-programming-for-windows-phone.aspx
Yay! :)
Its not a problem with emulator. you want to return the data from your HttpGet() method, but the data is already returned (as null) before the actual response from the webClient occurs. hence I suggest you to make some changes to the code and try.
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(new Uri(site, UriKind.Absolute));
and then in the DownloadCompleted event handler(or callback), you manupulate the actual result
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
var response= e.Result; // Response obtained from the site
}
Related
I am using WebClient to download some stuff from internet in Windows Phone 8.1 app.
Below is the sample code i am using in my app - where i am calling below method, but my webclient is not waiting to complete the read operation and returning immediately after OpenReadAsync call.
how can i make sure that my method return operation must wait till OpenReadCompleted event is completed?
I have seen multiple similar questions, but couldn't find a solution.
MyCustomObject externalObj; // my custom object
private static void CheckNetworkFile()
{
try
{
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += (s, e) =>
{
externalObj = myReadWebclientResponse(e.Result); // my custom method to read the response
};
webClient.OpenReadAsync(new Uri("http://externalURl.com/sample.xml", UriKind.Absolute));
}
catch (Exception)
{
externalObj = null;
}
}
I would advise you to use WebClient.OpenReadTaskAsync with a combination of the async/await keywords introduced in .NET 4.5 instead. You need to add the async keyword to your method, make it return a Task and it is advisable to end your method with the Async postfix:
MyCustomObject externalObj;
private static async Task CheckNetworkFileAsync()
{
try
{
WebClient webClient = new WebClient();
Stream stream = await webClient.OpenReadTaskAsync(new Uri("http://externalURl.com/sample.xml", UriKind.Absolute));
externalObj = myReadWebclientResponse(stream);
}
catch (Exception)
{
externalObj = null;
}
}
Edit:
As you said, WebClient.OpenReadTaskAsync isn't available for WP8.1, So lets create an Extension Method so it will be:
public static class WebClientExtensions
{
public static Task<Stream> OpenReadTaskAsync(this WebClient client, Uri uri)
{
var tcs = new TaskCompletionSource<Stream>();
OpenReadCompletedEventHandler openReadEventHandler = null;
openReadEventHandler = (sender, args) =>
{
try
{
tcs.SetResult(args.Result);
}
catch (Exception e)
{
tcs.SetException(e);
}
};
client.OpenReadCompleted += openReadEventHandler;
client.OpenReadAsync(uri);
return tcs.Task;
}
}
Now you can use it on your WebClient.
You can find great reading material in the async-await wiki and by simply filtering by that tag in the search bar.
I hope this is not too off topic, but others who are researching this might like to know that the above code sample can also be used for WCF calls in Silverlight. Be sure to add the Microsoft.Bcl.Async NuGet package first. Here is a WCF code example:
public static async Task<string> AuthenticateAsync(string userName, string password)
{
var dataAccessServiceClient = new DataAccessServiceClient("DataAccessService");
var taskCompletionSource = new TaskCompletionSource<string>();
EventHandler<AuthenticateCompletedEventArgs> completedHandler = null;
completedHandler = (s, args) =>
{
try
{
taskCompletionSource.SetResult(args.Result);
}
catch (Exception e)
{
taskCompletionSource.SetException(e);
}
};
dataAccessServiceClient.AuthenticateCompleted += completedHandler;
dataAccessServiceClient.AuthenticateAsync(userName, password);
return await taskCompletionSource.Task;
}
It can be called like this:
var result = await AuthenticateAsync(username, password);
I started Windows Phone programming with this example from Microsoft:
http://code.msdn.microsoft.com/wpapps/Hybrid-Web-App-75b7ef74/view/SourceCode
The app only displays the browser and load a URL.
Now I want to load an other URL directly from a .txt file.
For example: http://www.test.de/appurl.txt and then I want to load the URL in the Windows Phone App.
--> For example: http://anotherserver.de/index.html?mobileApp
My problem is, that the URL have to load synchronous and not asynchronous. I implement a AutoResetEvent, but it donĀ“t work...
Hope somebody can help me, thx!
Here is my Code:
public partial class MainPage : PhoneApplicationPage
{
// URL zur WebApp
// TODO: URL muss aus diesem TEXT-File ausgelesen werden!
private string _appURL = "http://www.test.de/appurl.txt";
public string _homeURL = "";
//private string _homeURL = "http://anotherserver.de/index.html?mobileApp";
// URL zur Registrierung von Angeboten
private string _registrationURL = "http://anotherserver.de/index.html?bereich=registrierung&mobileApp";
// Secondary tile data
//private Uri _currentURL;
//private Uri _tileImageURL;
//private string _pageTitle = "Shop ";
// Serialize URL into IsoStorage on deactivation for Fast App Resume
private Uri _deactivatedURL;
private IsolatedStorageSettings _userSettings = IsolatedStorageSettings.ApplicationSettings;
// To indicate when we're navigating to a new page.
private ProgressIndicator _progressIndicator;
// Constructor
public MainPage()
{
InitializeComponent();
//Read the URL from a txt file and set the _homeURL
ReadFile(_appURL);
// Setup the progress indicator
_progressIndicator = new ProgressIndicator();
_progressIndicator.IsIndeterminate = true;
_progressIndicator.IsVisible = false;
SystemTray.SetProgressIndicator(this, _progressIndicator);
// Event handler for the hardware back key
BackKeyPress += MainPage_BackKeyPress;
// Fast app resume events
PhoneApplicationService.Current.Deactivated += Current_Deactivated;
PhoneApplicationService.Current.Closing += Current_Closing;
}
//AutoResetEvent are = new AutoResetEvent(false);
public void ReadFile(string address)
{
var webClient = new WebClient();
webClient.OpenReadAsync(new Uri(address));
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
// lock the thread until web call is completed
//are.WaitOne();
//finally call the NotifyComplete method to end the background agent
//NotifyComplete();
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
try
{
using (var reader = new StreamReader(e.Result))
{
string downloaded = reader.ReadToEnd();
Debug.WriteLine("downloaded= " + downloaded);
_homeURL = downloaded;
//work = false;
}
}
catch
{
Debug.WriteLine("Please check your data connection");
MessageBox.Show("Please check your data connection");
}
//signals locked thread that can now proceed
//are.Set();
}
#region App Navigation Events
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
// Browser event handlers
Browser.Navigating += Browser_Navigating;
Browser.Navigated += Browser_Navigated;
Browser.NavigationFailed += Browser_NavigationFailed;
Browser.IsScriptEnabled = true;
// Try to get the URL stored for fast app resume.
try
{
_deactivatedURL = (Uri)(_userSettings["deactivatedURL"]);
}
catch (System.Collections.Generic.KeyNotFoundException keyNotFound)
{
Debug.WriteLine(keyNotFound.Message);
}
// Were we started from a pinned tile?
if (NavigationContext.QueryString.ContainsKey("StartURL"))
{
// Navigate to the pinned page.
Browser.Navigate(new Uri(NavigationContext.QueryString["StartURL"], UriKind.RelativeOrAbsolute));
}
else if ((_deactivatedURL != null) && (e.NavigationMode != NavigationMode.Reset))
{
// If there is a stored URL from our last
// session being deactivated, navigate there
if (Browser.Source != _deactivatedURL)
{
Browser.Navigate(_deactivatedURL);
}
}
else
{
// Not launched from a pinned tile...
// No stored URL from the last time the app was deactivated...
// So, just navigate to the home page
Browser.Navigate(new Uri(_homeURL, UriKind.RelativeOrAbsolute));
}
}
....
My problem is, that the URL have to load synchronous and not asynchronous
No you can't do it synchronously, but using async/await you can pretend it.
For this, You can use a method something like this (you can even write it as an extension method)
await Navigate(webBrowser1, "http://stackoverflow.com");
DoSomethingAfterNavigationCompleted();
Task Navigate(WebBrowser wb,string url)
{
var tcs = new TaskCompletionSource<object>();
WebBrowserDocumentCompletedEventHandler documentCompleted = null;
documentCompleted = (o, s) =>
{
wb.DocumentCompleted -= documentCompleted;
tcs.TrySetResult(null);
};
wb.DocumentCompleted += documentCompleted;
wb.Navigate(url);
return tcs.Task;
}
I am having a method which fetches HTML from a url, extracts entities by parsing it, and returns List of entites. Here is sample code:
public List<Entity> FetchEntities()
{
List<Entity> myList = new List<Entity>();
string url = "<myUrl>";
string response = String.Empty;
client = new WebClient();
client.DownloadStringCompleted += (sender, e) =>
{
response = e.Result;
// parse response
// extract content and generate entities
// <---- I am currently filling list here
};
client.DownloadStringAsync(new Uri(url));
return myList;
}
The problem is while async call is in progress control returns with empty myList. How can I prevent this. My ultimate goal is to return filled list.
And also this method is in a seperate class library project and being called from windows phone application and I have to keep it like that only. Is there any way to do this or I am missing something? Any help will be greatly appreciated.
You can either pass callback to the method like this and make it async without Tasks, so u have to update method usage slightly.
public void FetchEntities(
Action<List<Entity>> resultCallback,
Action<string> errorCallback)
{
List<Entity> myList = new List<Entity>();
string url = "<myUrl>";
string response = String.Empty;
client = new WebClient();
client.DownloadStringCompleted += (sender, e) =>
{
response = e.Result;
// parse response
// extract content and generate entities
// <---- I am currently filling list here
if (response == null)
{
if (errorCallback != null)
errorCallback("Ooops, something bad happened");
}
else
{
if (callback != null)
callback(myList);
}
};
client.DownloadStringAsync(new Uri(url));
}
The other option is to force it be synchronous. Like that
public List<Entity> FetchEntities()
{
List<Entity> myList = new List<Entity>();
string url = "<myUrl>";
string response = String.Empty;
client = new WebClient();
AutoResetEvent waitHandle = new AutoResetEvent(false);
client.DownloadStringCompleted += (sender, e) =>
{
response = e.Result;
// parse response
// extract content and generate entities
// <---- I am currently filling list here
waitHandle.Set();
};
client.DownloadStringAsync(new Uri(url));
waitHandle.WaitOne();
return myList;
}
That is the point of asynchronous programming to be non-blocking. You can pass a callback as a parameter and handle the result somewhere else instead of trying to return it.
If you need to return the result you can use this TPL library, I've been using it without problem for a while now.
public Task<string> GetWebResultAsync(string url)
{
var tcs = new TaskCompletionSource<string>();
var client = new WebClient();
DownloadStringCompletedEventHandler h = null;
h = (sender, args) =>
{
if (args.Cancelled)
{
tcs.SetCanceled();
}
else if (args.Error != null)
{
tcs.SetException(args.Error);
}
else
{
tcs.SetResult(args.Result);
}
client.DownloadStringCompleted -= h;
};
client.DownloadStringCompleted += h;
client.DownloadStringAsync(new Uri(url));
return tcs.Task;
}
}
And calling it is exactly how you use TPL in .net 4.0
GetWebResultAsnyc(url).ContinueWith((t) =>
{
t.Result //this is the downloaded string
});
or:
var downloadTask = GetWebResultAsync(url);
downloadTask.Wait();
var result = downloadTask.Result; //this is the downloaded string
Hope this helps :)
I have a simple webclient that connects to a webpage and returns the data. The code is as follows:
try
{
WebClient webClient = new WebClient();
Uri uri = new Uri("https://domain.com/register.php?username=" + txtbUser.Text);
webClient.OpenReadCompleted +=
new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(uri);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
//Process web service result here
MessageBox.Show(e.Result.ToString());
}
else
{
//Process web service failure here
MessageBox.Show(e.Error.Message);
}
}
The data coming from e.Result is MS.InternalMemoryStream and not the data coming back from the webpage, the data coming back from the webpage should just be a 0 or 1. Any idea's?
thanks,
Nathan
.ToString() returns the name of the class - in this case, InternalMemoryStream. You have to READ the stream to get the result. Check this out
I am using webrequest to fetch some image data. The url may be invaild sometime. In case of invalid URL, begingetresponse is taking time equals to timeout period. Also the control become unresponsive during that period. In other word the async callback is not working asynchronously. Is this expected behaviour?
try
{
// Async requests
WebRequest request = WebRequest.Create(uri);
request.Timeout = RequestTimeOut;
RequestObject requestObject = new RequestObject();
requestObject.Request = request;
request.BeginGetResponse(this.ProcessImage, requestObject);
}
catch (Exception)
{
ShowErrorMessage(uri);
}
private void ProcessImage(IAsyncResult asyncResult)
{
try
{
RequestObject requestObject = (RequestObject)asyncResult.AsyncState;
WebRequest request = requestObject.Request;
WebResponse response = request.EndGetResponse(asyncResult);
Bitmap tile = new Bitmap(response.GetResponseStream());
// do something
}
catch (Exception)
{
ShowErrorMessage();
}
}
looks like this is an issue with .NET. BeginGetResponse blocks until DNS is resolved. In case of wrong URL (like http://somecrap) it tries until it gets timeout. See the following links -
link1 and link2
I just ran into this same situation. While it's not a perfect workaround I decided to use the Ping.SendAsync() to ping the site first. Good part is the async part return immediately. Bad part is the extra step AND not all sites respond to Ping requests.
public void Start(WatchArgs args)
{
var p = new System.Net.NetworkInformation.Ping();
args.State = p;
var po = new System.Net.NetworkInformation.PingOptions(10, true);
p.PingCompleted += new PingCompletedEventHandler(PingResponseReceived);
p.SendAsync(args.Machine.Name, 5 * 1000, Encoding.ASCII.GetBytes("watchdog"), po, args);
}
private void PingResponseReceived(object sender, .PingCompletedEventArgs e)
{
WatchArgs args = e.UserState as WatchArgs;
var p = args.State as System.Net.NetworkInformation.Ping;
p.PingCompleted -= new System.Net.NetworkInformation.PingCompletedEventHandler(HttpSmokeWatcher.PingResponseReceived);
args.State = null;
if (System.Net.NetworkInformation.IPStatus.Success == e.Reply.Status)
{
// ... BeginGetResponse now
}
else
{
/// ... machine not available
}
}
Just code and running for a day but initial result look promising.