Passing data to ui via event handler in Windows Phone 8

674 Views Asked by At

I'm using the following pattern to pass response data of an HTTP request(runs on a worker thread) to the concerning UI.

using System;

namespace ConsoleApplication1
{

    class Program
    {
        static void Main(string[] args)
        {
            Counter c = new Counter(new Random().Next(10));
            c.ThresholdReached += c_ThresholdReached;

            Console.WriteLine("press 'a' key to increase total");
            while (Console.ReadKey(true).KeyChar == 'a')
            {
                Console.WriteLine("adding one");
                c.Add(1);
            }
        }

        static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e)
        {
            Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold,  e.TimeReached);
            Environment.Exit(0);
        }
    }

    class Counter
    {
        private int threshold;
        private int total;

        public Counter(int passedThreshold)
        {
            threshold = passedThreshold;
        }

        public void Add(int x)
        {
            total += x;
            if (total >= threshold)
            {
                ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
                args.Threshold = threshold;
                args.TimeReached = DateTime.Now;
                OnThresholdReached(args);
            }
        }

        protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
        {
            EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;
    }

    public class ThresholdReachedEventArgs : EventArgs
    {
        public int Threshold { get; set; }
        public DateTime TimeReached { get; set; }
    }
}

When I debug the code, I observed that event is sent to it's receiver UI succesfully,but with empty data. I also monitored that data is put correctly to the event just before the event is sent. Seems like data is disappeared on the fly.

Any ideas?


Ok here is my code;

Step 1: In the UI's background worker I do the following;

    private void ButtonSend_Click(object sender, RoutedEventArgs e)
    {
        // Create a worker thread
        worker = new BackgroundWorker();
        worker.DoWork += new DoWorkEventHandler(worker_DoWork);
        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_DoWorkFinished);
        worker.WorkerSupportsCancellation = true;
        worker.RunWorkerAsync();
    }

    ....


    void worker_DoWork(object sender, DoWorkEventArgs e)
    {

         ApplicationManager.Instance.getHTTPManager().makeHTTPSRequest(url);

    }

Step 2: Then in the HTTPManager, after the WebClient finishes it's POST request I do the following;

            webClient.WriteStreamClosed += (s, args) =>
            {
                if (args.Error != null)
                {
                    status = mStatusFromServer;
                }
                else
                {
                    status = HTTPManager.mDefaultStatus;
                }

                ContactSendCompletedEventArgs args2 = new  ContactSendCompletedEventArgs();
                args2.Status = status;
                Debug.WriteLine("Status: " + args2.Status);
                OnContactSendCompleted(args2);
                //Debug.WriteLine("Status: " + status);
                //Contact_Send_Complete(this, status, i);
            };

I also did the event handling in this class like;

 namespace MyPhone8App.Common
 {

public delegate void ContactSendCompletedEventHandler(ContactSendCompletedEventArgs e);

class HTTPManager
{

    ...

    protected virtual void OnContactSendCompleted(ContactSendCompletedEventArgs e)
    {
        EventHandler<ContactSendCompletedEventArgs> handler = ContactSendCompleted;
        if (handler != null)
        {
            handler(this, e);
        }
    }

    public event EventHandler<ContactSendCompletedEventArgs> ContactSendCompleted;
}     

public class ContactSendCompletedEventArgs : EventArgs
{
    public String Status { get; set; }
}

Final Step: Back in UI I do the following;

        HTTPManager man = ApplicationManager.Instance.getHTTPManager();
        man.ContactSendCompleted += c_Contact_Send_Completed;

    static void c_Contact_Send_Completed(object sender, ContactSendCompletedEventArgs args)
    {
        Debug.WriteLine("The status: ", args.Status);

        ...

    }

And that's all. Any ideas?

0

There are 0 best solutions below