Solved

holding data from text file read from web address

Posted on 2011-03-05
5
631 Views
Last Modified: 2012-05-11


in the code below, the last subroutine assigns e.Result to a variable readFile that is defined just initially.

The problem is that I can not get the value of e.Result in a variable so I can use it somewhere else.  I've tried a string array storage class with no luck as well.  I've tested the value of readFile inside the client_DownloadStringCompleted routine and it is getting the value.

see the remarks with asterisks *******

    
public partial class MainPage : PhoneApplicationPage
    {
       string readFile;  


  public MainPage()
        {      
              pullSchedData();
             textBlock1.Text = readFile; //******** this does not work, readFile is empty now********

          public void pullSchedData()
        {
            client.DownloadStringAsync(new Uri("http://www.webaddress/sched.txt"), "mypage");
        }
       

public void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            if (e.UserState as string == "mypage")
            {
                textBlock1.Text = e.BytesReceived.ToString() + " bytes received.";
            }
         }
 

 public void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
                                 
            if (e.Error == null && !e.Cancelled) readFile = e.Result;
            textBlock1.Text = readFile; //****** this works, but if I check the value of readFile outside of this routine, it is empty*****

        }
}

Open in new window

0
Comment
Question by:Zipbang
  • 3
  • 2
5 Comments
 
LVL 10

Expert Comment

by:Jacco
ID: 35045170
It does not work because you use the Async version of DownloadString.

PullSchedData() will return immediately and the web request is done on a different thread.

You can either use the non-async version or set a boolean to true client_DownloadStringCompleted to let your application know the data has been retrieved.

Regards, Jacco
0
 

Author Comment

by:Zipbang
ID: 35046967
@Jacco:

Thank you for your quick response.  You certainly have the answer, I am just having a hard time finding a way to implement it.

I've not found the alternative to the Async version of DownloadString.

I've searched around and have had difficulty finding out to check client_DownloadStringCompleted other than how it is done in the example I pasted?

I appreciate your help.
0
 
LVL 10

Expert Comment

by:Jacco
ID: 35054000
The synchronous alternative would be:

        public void pullSchedData()
        {
            client.DownloadString(new Uri("http://www.webaddress/sched.txt"));
        }

Am not sure if progress event will be called in that case though.

Regards, Jacco
0
 

Author Comment

by:Zipbang
ID: 35063203
@Jacco:

Thanks again, and again, you'll get the points for this, but I see now I wasn't clear enough in my original question as I only put it in the tags.

It's for Windows Phone 7 and client.DownloadString is not supoorted in WP7 for some reason
0
 
LVL 10

Accepted Solution

by:
Jacco earned 500 total points
ID: 35067081
Ah ok. That is somewhat different then :-)

There is a way to convert a Asynchronous call to Synchronous.

I have checked if the used methods are available in the Compact Framework.

Regards, Jacco
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading;

namespace AsyncTest
{
    class Program
    {
        public static string result;

        static void Main(string[] args)
        {
            // create the client
            var client = new WebClient();
            // set DownloadStringAsync callback
            client.DownloadStringCompleted += client_DownloadStringCompleted;
            // create a signal
            var s_Signal = new EventWaitHandle(false, EventResetMode.ManualReset);
            // call DownloadStringAsync passing the signal
            client.DownloadStringAsync(new Uri("http://www.google.com"), s_Signal);
            // wait until signalled
            s_Signal.WaitOne();
            // because of the WaitOne result will always be set: to test uncomment code below
            // Console.WriteLine(result);
            // Console.ReadLine();
        }

        public static void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null && !e.Cancelled) 
                result = e.Result;
            var signal = (EventWaitHandle)e.UserState;
            signal.Set();
        }
    }
}

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Let Bitmoji into your life. Now is the time to learn a new language of smartphone messaging with this brief introduction.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question