You can't without "hacks" and you shouldn't - embrace asynchrony and pass in a delegate that you want to be executed once the download is completed.
Up vote 0 down vote favorite 1 share g+ share fb share tw.
Here is the function I have now that obviously doesn't work. The reason it doesn't work is because WebClient is asynchronous and data is empty before it gets filled by WebClient and crashes on the XML reader. How can I call WebClient within this function and still allow it to return ServerResult as required with or without needing an external event handler?
Static public ServerResult isBarcodeCorrectOnServer(string barcode) { Dictionary dict = configDictionary(); string urlString = (string. Format("myurl.com/app/getbarcodetype.php?realbar..., barcode, dict"type")); WebClient wc = new WebClient(); string data = ""; wc. DownloadStringCompleted += (sender, e) => { if (e.
Error == null) { //Process the result... data = e. Result; } }; wc. DownloadStringAsync(new Uri(urlString)); StringReader stream = new StringReader(data); var reader = XmlReader.
Create(stream); var document = XDocument. Load(reader); var username = document. Descendants("item"); var theDict = username.Elements().
ToDictionary(ev => ev.Name. LocalName, ev => ev. Value); if (theDict.
ContainsKey("type") == true && theDict"type".ToString() == dict"type".ToString()) { return ServerResult. KOnServer; } else if (theDict. ContainsKey("type") == true) { return ServerResult.
KWrongType; } else { return ServerResult. KNotOnServer; } } c# .net visual-studio silverlight windows-phone-7 link|improve this question asked Feb 22 at 22:07Ethan Allen444211 80% accept rate.
– MarcinJuraszek Feb 22 at 22:11 3 Silverlight does not support non-async – BrokenGlass Feb 22 at 22:13.
You can't without "hacks" and you shouldn't - embrace asynchrony and pass in a delegate that you want to be executed once the download is completed: static public void isBarcodeCorrectOnServer(string barcode, Action completed) { //.. wc. DownloadStringCompleted += (sender, e) => { if (e. Error == null) { //Process the result... data = e.
Result; completed(data); } }; //.. } You can move all your processing code now into a separate method which you call with the download result.
Passing in the delegate is what I needed to know. Thanks! – Ethan Allen Feb 22 at 22:47.
You can't, basically. Or at the very least you shouldn't. You've got a method which is designed to be synchronous, on a platform which is designed for asynchronous IO.
Fundamentally, you should design your code to work with the platform. Accept that it will be asynchronous, and make the calling code deal with that. Note that when C# 5 comes out and when it's supported by Windows Phone, all of this will be a lot simpler using async.
You'd return a Task from the method, and await the result of the WebClient. If you're only developing for fun (so don't mind working with a CTP which has some bugs, and may not be valid to ship for marketplace apps) you can do that today.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.