zoukankan      html  css  js  c++  java
  • Silverlight 3 MultiThreading编程http://blog.csdn.net/zjfei/archive/2009/07/27/4384428.aspx

    多线程对开发和用户体验的重要性不言而喻,Silverlight BCL 几乎提供了完整的 Thread Class。

    1. Dispatcher

    和 WPF / WinForm 一样,我们只能在 UI Thread 中更新显示控件属性。多线程编码时,需要借助于 Dispatcher。

    private void Button_Click(object sender, RoutedEventArgs e)
    {
      new Thread(() =>
      {
        this.Dispatcher.BeginInvoke(() =>
        {
         this.TextBlock1.Text = DateTime.Now.ToString();
        });
      }).Start();
    }

    在类库中可以用 Deployment.Current.Dispatcher 来获取 Dispatcher 引用。

    private void Button_Click(object sender, RoutedEventArgs e)
    {
      new Thread(() =>
      {
        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
         this.TextBlock1.Text = DateTime.Now.ToString();
        });

      }).Start();
    }

    当然,也可以用同步上下文来完成类似操作。

    private void Button_Click(object sender, RoutedEventArgs e)
    {
      var context = SynchronizationContext.Current;

      new Thread(() =>
      {
        context.Send((s) =>
        {
          this.TextBlock1.Text = DateTime.Now.ToString();
        }, null);
      }).Start();
    }

    2. ThreadPool

    Silverlight ThreadPool 默认线程数量为:

    WorkerThreads: 2 ~ 500
    CompletionPortThreads: 2 ~ 1000

    使用方法和以往并无两样。

    private void Button_Click(object sender, RoutedEventArgs e)
    {
      ThreadPool.QueueUserWorkItem((s) =>
      {
        this.Dispatcher.BeginInvoke(() =>
        {
          int minWorkerThreads, minCompletionPortThreads, maxWorkerThreads, maxCompletionPortThreads;
         
          ThreadPool.GetMinThreads(out minWorkerThreads, out minCompletionPortThreads);
          ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxCompletionPortThreads);

          this.TextBox1.Text = String.Format("WorkerThreads = {0} ~ {1}, CompletionPortThreads = {2} ~ {3}",
            minWorkerThreads, maxWorkerThreads, minCompletionPortThreads, maxCompletionPortThreads);
        });
      });
    }

    需要注意的是:尽管 ThreadPool 提供了 SetMinThreads 、SetMaxThreads 方法,但却无法使用,调用会触发异常。

    说到线程池,自然会想到委托的异步调用,不过这同样是个问题。

    In Silverlight, using delegates to make asynchronous method calls is not supported. Calling BeginInvoke causes a NotSupportedException.

    3. WaitHandle

    等待句柄是多线程编程必不可少的装备。

    public partial class MainPage : UserControl
    {
      AutoResetEvent handle = new AutoResetEvent(true);

      public MainPage()
      {
        InitializeComponent();
     
        new Thread(() =>
        {
          while (true)
          {
            handle.WaitOne();

            this.Dispatcher.BeginInvoke(() =>
            {
              this.TextBlock1.Text = DateTime.Now.ToString();
            });
          }
        }).Start();
      }

      private void Button_Click(object sender, RoutedEventArgs e)
      {
        handle.Set();
      }
    }

    4. Timer

    System.Thread.Timer 是一种多线程定时器。

    public partial class MainPage : UserControl
    {
      Timer timer;

      public MainPage()
      {
        InitializeComponent();

        timer = new Timer((state) =>
        {
          this.Dispatcher.BeginInvoke(() =>
          {
            this.TextBlock1.Text = DateTime.Now.Ticks.ToString();
          });
        }, null, 0, 100);
      }
    }

    TimerCallback 是在线程池中执行的,这意味着它并不会等待事件代码执行完成就会触发下一次事件,完全取决于代码执行时间是否小于间隔时间。

    另外还有一个 System.Windows.Threading.DispatcherTimer,它直接使用 Dispatcher Queue 来执行事件代码,因此可以直接更新界面元素。

    public partial class MainPage : UserControl
    {
      DispatcherTimer timer;

      public MainPage()
      {
        InitializeComponent();

        timer = new DispatcherTimer();
        timer.Tick += (s, e) => { this.TextBlock1.Text = DateTime.Now.ToString(); };
        timer.Interval = TimeSpan.FromMilliseconds(1000);
        timer.Start();
      }
    }
    【reprinted from http://www.rainsts.net/


    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zjfei/archive/2009/07/27/4384428.aspx

  • 相关阅读:
    ADB命令大全
    Backup your Android without root or custom recovery -- adb backup
    Content portal for Pocketables Tasker articles
    Is there a way to detect if call is in progress? Phone Event
    Tasker to proximity screen off
    Tasker to detect application running in background
    Tasker to create toggle widget for ES ftp service -- Send Intent
    Tasker to proximity screen on
    Tasker to answer incoming call by pressing power button
    Tasker to stop Poweramp control for the headset while there is an incoming SMS
  • 原文地址:https://www.cnblogs.com/si812cn/p/1553662.html
Copyright © 2011-2022 走看看