1

我正在使用以下模式将 HTTP 请求的响应数据(在工作线程上运行)传递给相关的 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; }
    }
}

当我调试代码时,我观察到事件已成功发送到它的接收器 UI,但数据为空。我还监控到在事件发送之前数据是否正确放入事件中。似乎数据在飞行中消失了。

有任何想法吗?


好的,这是我的代码;

第 1 步:在 UI 的后台工作人员中,我执行以下操作;

    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);

    }

第 2 步:然后在 HTTPManager 中,WebClient 完成它的 POST 请求后,我执行以下操作;

            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);
            };

我也在这个类中做了事件处理,比如;

 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; }
}

最后一步:返回 UI 我执行以下操作;

        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);

        ...

    }

就这样。有任何想法吗?

4

0 回答 0