?
Solved

holding data from text file read from web address

Posted on 2011-03-05
5
Medium Priority
?
641 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Let me explain this picture a little bit.  First, in case you haven't already guessed, you are looking at my 2 phones, an Android Samsung Galaxy S5 on the left and an iPhone 5 on the right.  They are on their respective cradles on my desk.  But, you…
As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

770 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