Atul Goel
Atul Goel

Reputation: 18

Why doesn't delegate call the function?

1: Can someone explain the last line of the first function to me?

2: The second function doesn't work. Please tell me why.The PHP script is fetching the data.

I edited the code to get this, but the app now crashes with a System nullreferenceexception. Please help.

    private void checkbutton_Click(object sender, RoutedEventArgs e)
    {
        statustext.Text = "Checking for new score";
        var webclient = new WebClient();
        webclient.OpenReadCompleted += new OpenReadCompletedEventHandler(getscores_OpenReadCompleted);
        webclient.OpenReadAsync(new Uri("http://example.com/get.php?"+DateTime.Now));


    }
    void getscores_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        StreamReader s = null;
        Stream reply=null;
        try
        {

               reply = (Stream)e.Result;
                 s = new StreamReader(reply);                  

            }

        catch 
        {
            statustext.Text = "ERROR IN FETCHING";
        }
        scorebox.Text = s.ReadToEnd();
        statustext.Text = "DoNE";

    }

Upvotes: 0

Views: 116

Answers (2)

Servy
Servy

Reputation: 203827

The last line of the first method is attaching a handler to an event. It is saying that when the OpenReadCompleted event fires, that is to say when the read completes, the getscores_OpenReadCompleted method should be called.

The getscores_OpenReadCompleted doesn't work because it's trying to access a UI element from a non-UI thread.

You're also adding the handler after starting the asynchronous operation, so while it's unlikely, it's certainly possible that the operation completes very quickly and the event is fired before you add the handler. While this situation would be very unusual, it's fixed very quickly and easily by simply adding the handler before you start the asynchronous operation.

Upvotes: 6

xDaevax
xDaevax

Reputation: 2022

There are a couple of issues here:

  1. Register the delegate before the call to OpenReadAsync
  2. Read the stream from the event arguments and close the stream when you're done.

private void checkbutton_Click(object sender, RoutedEventArgs e)
{
    statustext.Text = "Checking for new score";
    var webclient = new WebClient();
    webclient.OpenReadCompleted += new OpenReadCompletedEventHandler(getscores_OpenReadCompleted);
    webclient.OpenReadAsync(new Uri("http://example.com/get.php?"+DateTime.Now));

}
void getscores_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    Stream reply = null;
    StreamReader s = null;
    string outputText = string.Empty;
    try
    {
        reply = (Stream)e.Result;
        s = new StreamReader(reply);
        outputText = s.ReadToEnd();
    }
    finally
    {
        if (s != null)
        {
            s.Close();
        }

        if (reply != null)
        {
            reply.Close();
        }
    }
    statustext.Text = outputText;

}

See the usage of the OpenReadAsync method here:

Upvotes: 0

Related Questions