0

hello friends i have one code which is as follows :

for (int i = 1; i < 6; i++) { int j = 0; Nos[j++] = Config[i]; var xmladd = "uri to download data"; WebClient _proxy2 = new WebClient(); _proxy2.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCompleted); _proxy2.DownloadStringAsync(new Uri(xmladd)); string msg = Config[11] + ":" + Config[12] + " .My Current Location is " + Properties.address + " , Latitude : " + clslatlong.ReturnLat() + " , Longitude : " + clslatlong.ReturnLongi(); } 

here problem is that completed event is not fired immedietely and keep on executing the further code but my next code is based on the result returned by the completed event what should be done in this kinda situation ? my code should wait to get the response from completed event and then proceed further please help.

5
  • I agree with Dave, async should be "fire and forget", if you need to wait for it to finish before proceeding, you should be calling it syncronously. Commented Jan 4, 2013 at 17:06
  • 1
    @Kevin Unless you need to not block the current thread, which is a very common use case. Commented Jan 4, 2013 at 17:07
  • yeah agree , i know , then how do i call methods mentioned in the code synchronously in wp7 application ? Commented Jan 4, 2013 at 17:13
  • You have a DownloadStringCompleted event handler... Why can't you use that to create your string msg using your Properties.address, etc? Commented Jan 4, 2013 at 17:32
  • @DaveZych ok yeah got a hint let me try hope it works well.. Commented Jan 4, 2013 at 17:45

3 Answers 3

2

You should continue your code execution in the request complete handler, if it's depending on the result. This is how event driven or async coding works.

Sign up to request clarification or add additional context in comments.

3 Comments

u didn't get my problem. in my completed event i set Properties.address ok but i will get null value as completed event does not fire instead it continuing code execution.
It seems there's something wrong in the async call that you try to make if you get null as a result or your completion handler is not called. Anyway, you cannot (shouldn't) wait for a result in the method that calls the async method.
@DharaPPatel I think you misunderstand how DownloadStringAsync() (and asynchronous calls in general) work. It doesn't download the data, call your callback delegate and then return. What it does is that it returns immediately and then it (asynchronously) downloads the data and calls your callback.
1

Asynchronous programming can get messy; the correct way to do it is via Johan Paul's answer: split up your function and put part of it in the event handler. Your code will end up quite messy because you're performing an asynchronous operation in a loop.

Your question has both WP7 and WP7.1 tags; if you can get away with just supporting WP7.1, then you have the option of the Microsoft.Bcl.Async library (currently in Beta). This enables async/await on WP7.1, which is much easier:

for (int i = 1; i < 6; i++) { int j = 0; Nos[j++] = Config[i]; var xmladd = "uri to download data"; WebClient _proxy2 = new WebClient(); var result = await _proxy2.DownloadStringTaskAsync(xmladd); ... } 

Comments

0

You can use Async CTP library for such features.

Download: http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=9983

Examples: http://10rem.net/blog/2012/05/22/using-async-and-await-in-silverlight-5-and-net-4-in-visual-studio-11-with-the-async-targeting-pack

9 Comments

Does that work for WP7? And I wouldn't recommend using a CTP (a preview) if possible, it has several known bugs.
Yes it work in WP7 too. kevinashley.com/… Also, I want to stress that microsoft build all the async operations of future products like win8 RTM on that tast based async architecture. Please read following readings: mnajder.blogspot.com/2011/01/async-ctp-on-wp7.html msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx
If you do not prefer to use CTP, then you will always be late for the future and build your infrastructure always on the outdated/antiquated technology. Please read following readings: mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/… blogs.msdn.com/b/windowsappdev/archive/2012/04/24/… msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx silverlightshow.net/items/…
-Compatible with Visual Studio 2010 SP1, which is now required. -Compatible with non-English installations of Visual Studio and with Visual Studio Express. -Compatible with Silverlight 5. -Compatible with Windows Phone SDK 7.1 (Mango). -Compatible with Roslyn CTP
"If you do not prefer to use CTP, then you will always be late for the future and build your infrastructure always on the outdated/antiquated technology." I do use CTPs to learn about upcoming technologies, but I would be extremely careful about using them in production. And are you really saying that VS 2012 is an outdated technology and that I should be using VS 2010 with the CTP instead?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.