Solved

holding data from text file read from web address

Posted on 2011-03-05
5
626 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
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Comment Utility
@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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

If you’re having problems with your Nexus 4, you’ll want to take a look at this step-by-step troubleshooting guide that walks you through common issues and helps you solve them so that you can extend the life of your device.  Phone Won't Turn On…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now