【问题标题】:Silverlight/Windows Phone 7 Equivalent of android.os.HandlerSilverlight/Windows Phone 7 等效于 android.os.Handler
【发布时间】:2011-06-22 15:44:45
【问题描述】:

Android 平台有一个 Handler 类,用于将消息或事件排队,以便在稍后阶段或在不同的线程上运行。我在 MSDN 文档和网络上寻找 Windows Phone 7 平台上可用的一些等效 API,但没有找到任何东西。

我可以自己实现该服务,但不愿意重新发明轮子。有没有人发现类似的东西或有什么好主意?

干杯, 阿拉斯代尔。

【问题讨论】:

    标签: android windows-phone-7 message-queue


    【解决方案1】:

    这里是代码的要点。你肯定需要做出改变。希望对您有所帮助。

       private static Queue<Dictionary<string, string>> messages;
       ....
       {   
            ...
            AutoResetEvent ev = new AutoResetEvent(false);
            ...
            Dictionary<string, string> msg1 = new Dictionary<string, string>();
            msg1.Add("Id", "1");
            msg1.Add("Fetch", "Song1");
            Dictionary<string, string> msg2 = new Dictionary<string, string>();
            msg2.Add("Id", "2");
            msg2.Add("Fetch", "Song2");
    
            messages.Enqueue(msg1);
            messages.Enqueue(msg2);
    
            ThreadPool.RegisterWaitForSingleObject(
                ev,
                new WaitOrTimerCallback(WaitProc),
                messages,
                5000,
                false
            );
    
            // The main thread waits 10 seconds, to demonstrate the
            // time-outs on the queued thread, and then signals.
            Thread.Sleep(10000);
            ...
        }
        private static void WaitProc(object state, bool timedOut)
        {
            // The state object must be cast to the correct type, because the
            // signature of the WaitOrTimerCallback delegate specifies type
            // Object.
            Queue<Dictionary<string, string>> dict = (Queue<Dictionary<string, string>>)state;
    
            string cause = "TIMED OUT";
            if (!timedOut)
            {
                cause = "SIGNALED";
                //signaled to return. return without doing any work
                return;
            } 
            // timed out. now do the work 
            Dictionary<string, string> s1 = dict.Dequeue();
        }
    

    `

    【讨论】:

    • 我不需要提供完整的代码示例来回答我的问题。建议 ThreadPool.RegisterWaitForSingleObject 方法调用就足够了。还是谢谢。
    • @ajmccall,我一定是误会了。由于我在之前的回复中提到了ThreadPool.RegisterWaitForSingleObject,我认为您正在寻找更多详细信息。无论如何,很高兴这有帮助。
    • 我不认为这个答案是线程安全的。 Queue 对象正在跨线程共享,但文档表明 Queue 的实例不是自动线程安全的:msdn.microsoft.com/en-us/library/system.collections.queue.aspx 此外,此实现与 Handler 有点不同,因为在这种情况下它会一遍又一遍地等待 5 秒,处理程序更灵活,它不需要每条消息在被处理之前等待完全相同的时间。
    【解决方案2】:

    不确定您是在寻找在后台执行某事还是在线程上执行某事的服务。你看过ThreadPool 类的线程吗?

    您可以使用ThreadPool.QueueUserWorkItem 在不同的线程上运行或使用 ThreadPool.RegisterWaitForSingleObject 注册一个委托以等待超时并稍后运行。

    【讨论】:

    • 感谢@Vivek 我知道这些 API 并一直在使用它们来实现所需的行为。使用 Handler 实现的好处是您可以免费获得消息/操作的排队,以及在必要时取消队列的能力。
    • 好的,我明白了。诚然,在 Android 的情况下,您可以安排和传递消息。我相信你已经考虑过了——在 RegisterWaitForSingleObject 中传递一个字典项队列。可能会做类似的事情吗?
    • 您还需要查找 Dispatcher.BeginInvoke 以在主线程上执行代码(以显示您的计算结果、网络调用等)。
    • 是的,谢谢,这正是我所需要的。我看到了这个 API,但无法通读编程术语来了解它的真正作用。请编辑您的答案,我会将其标记为正确。干杯
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多