线程 Wp7 与 Android 的比较 [英] Threading Wp7 in comparison with Android

查看:21
本文介绍了线程 Wp7 与 Android 的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个 Android 应用程序移植到 Windows Phone 7,在仔细阅读 System.Threading 命名空间文档以及网络上的各种教程和示例时,我仍然遗漏了一些花絮.

I'm porting an Android app to Windows Phone 7, and while perusing through the System.Threading namespace docs and various tutorials and examples on the web, I am still missing some tidbits.

到目前为止我学到了什么:

What I have learned so far:

1、创建线程并在该线程上执行函数:

1, To create a thread and execute a function on that thread:

    using System.Threading;

    ...

    Thread newThread = new Thread(new ThreadStart(MyMethod()));
    newThread.Start();

2、同步对象(类似于Android的synchronized()关键字):

2, To synchronize objects (analogous to Android's synchronized() keyword):

    lock(this)
    {
      // Do synchronized stuff here.
    }

我缺少什么:

3、如何从我的工作线程在调用线程上调用委托?调用线程"是指创建我的工作线程的线程,而不是工作线程本身.例如,在 Android 上,我会执行以下操作(这将在主 ui 线程上调用它,这也是一个可接受的解决方案):

3, How can I call a delegate from my worker thread on the calling thread? By "calling thread" I mean the thread that created my worker thread, not the worker thread itself. For example, on Android I would do the following (which would call it on the main ui thread, also an acceptable solution):

    Message.obtain(m_messageHandler, message, null).sendToTarget();

我找到了 AsyncCallback.这能做我需要的吗?有人有例子吗?哪个线程调用回调例程不是很清楚.

I have found AsyncCallback. Does this do what I need it to? Anyone have an example? It's not very clear as to what thread calls the callback routine.

4、在Android上,工作线程可以做一个wait()来暂停,直到调用线程调用interrupt().这类似于 win32 中的 Event 和 WaitForObject().例如在 Android 上:

4, On Android, the worker thread can do a wait() to pause until the calling thread calls interrupt(). This is similar to an Event and WaitForObject() in win32. For example on Android:

    // Worker thread does:
    synchronized (this)
    {
      wait();
    }

    ...

    // Main thread calls:
    synchronized(this) 
    {
      if(m_thread != null)
      {
        // Interrupt any wait()s.
        m_thread.interrupt();
      }
    }

一旦线程被中断,工作线程就会在 wait() 调用后继续执行.非常适合服务类型的线程.

As soon as the thread is interrupted, the worker thread continues executing after the wait() call. Great for service-type threads.

非常感谢有关 #3 和 #4 的任何指导,谢谢!

Any guidance on #3 and #4 would be much appreciated, thanks!

推荐答案

另一种在另一个线程上启动的方法是

Another way to kick something off on another thread is

ThreadPool.QueueUserWorkItem(state => {
  // do work here
}, myStateObjectOrNull);

我发现这种语法比创建 Thread 更容易.

I find this syntax easier than creating a Thread.

为了在 UI 线程上工作,这对于更新 UI 至关重要,您需要 Dispatcher 的句柄.最简单的获取方式如下

To so work back on the UI thread, which is essential for anthing that will update the UI, you need a handle to the Dispatcher. The easiest way to get one is as follows

Deployment.Current.Dispatcher.BeginInvoke(() => {
  // do work here
});

您不能在 WP7 上等待.您需要使用回调、事件或反应式扩展.通过 API,您可以通过多种方式编写等待的代码,但它只会锁定应用,而等待会阻止正在等待的代码运行.

You can not wait on WP7. You need to work with callbacks, events or Reactive Extensions. There are a couple of ways through the API where you can write code that waits, but it will just lock up the app and the waiting prevents the code it is waiting for to run.

这篇关于线程 Wp7 与 Android 的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆