Solved

holding data from text file read from web address

Posted on 2011-03-05
5
632 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c# - Best approach for objects in functions 3 28
How to read text with RegEx... 2 35
Visual Studio hangs on running project 6 61
"The resource cannot be found" 1 24
If you are anything like me, you install many apps on your phone and have your life on it, sometimes literally.  When I bought my current phone, a Samsung Galaxy S5 from Verizon, they were only selling the versions with 16 GB internal.  I didn't rea…
The case of the missing phone talks about the way a small electronic gadget (the mobile phone) has penetrated into our lives and has made us addicted to it.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

733 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