多线程对开发和用户体验的重要性不言而喻,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();
}
{
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();
}
{
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();
}
{
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
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.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();
}
}
{
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);
}
}
{
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();
}
}
{
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/】