Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 643
  • Last Modified:

holding data from text file read from web address



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
Zipbang
Asked:
Zipbang
  • 3
  • 2
1 Solution
 
JaccoCommented:
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
 
ZipbangAuthor Commented:
@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
 
JaccoCommented:
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
 
ZipbangAuthor Commented:
@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
 
JaccoCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now