1

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?

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
stdout
  • 2,471
  • 2
  • 31
  • 40
  • You should post the code with the problem of your winphone 8 app. – Alaa Masoud Aug 24 '13 at 09:09
  • Sorry but it'n not available now. But for now I can detail what I did more clearly. I have a UI Page that triggers BackgroundWorker in which I perform an HTTPS request using WebClient inside MyHTTPManager class. (This class is where I defined my EventHandler as well.) In UI Page I subscribe to the event like; ApplicationManager.Instance.getHTTPManager().My_Event += Event_Handler_CallBack_In_Page. In Weblient OpenWriteClosed() method, I get the status of the response and passing it to the UI. Although it enters the callback,no data semmed to be passed. Hope this helps a bit. – stdout Aug 24 '13 at 09:54
  • Define "empty data." Is the parameter `e` null? – lsuarez Aug 24 '13 at 11:31
  • No, not at all. Please see my answer – stdout Aug 26 '13 at 06:52

0 Answers0