Silverlight 3 - MultiThreading编程

Silverlight 3 - MultiThreading编程,第1张

概述多线程对开发和用户体验的重要性不言而喻,Silverlight BCL 几乎提供了完整的 Thread Class。 1. Dispatcher 和 WPF / WinForm 一样,我们只能在 UI Thread 中更新显示控件属性。多线程编码时,需要借助于 Dispatcher。 private void Button_Click(object sender, RoutedEventArgs

多线程对开发和用户体验的重要性不言而喻,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,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,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/】 总结

以上是内存溢出为你收集整理的Silverlight 3 - MultiThreading编程全部内容,希望文章能够帮你解决Silverlight 3 - MultiThreading编程所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1049054.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存