时间:2011-05-16 05:35来源:IT168 作者:朱先忠
@H_403_29@ 1.使用WaitHandle @H_403_29@ 等待句柄应当是你进行多线程编程的必备装备。由于我们的主要兴趣点在于Silverlight多线程编程相关的内容,所以我们不想再深入探讨WaitHandle。但在此为你提供一个典型的例子,告诉你使用WaitHandle的基本方法。 @H_403_29@ 清单1: 以下是代码片段: 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(); } } |
以下是代码片段: namespace SilverlightmultiThread { public partial class TimerTestPage : Page { System.Threading.SynchronizationContext _syncContext; System.Threading.Timer _timer; private int _flag = 0; public TimerTestPage() { { InitializeComponent(); //UI线程 _syncContext = System.Threading.SynchronizationContext.Current; //输出当前时间 txtMsg.Text = DateTime.Now.ToString() + "\r\n"; _timer = new System.Threading.Timer(MyTimerCallback,"helltimer",3000,1000); } private voID MyTimerCallback(object state) { string result = string.Format("{0} - {1}: \r\n",DateTime.Now.ToString(),(string)state); _syncContext.Post(delegate { txtMsg.Text += result; },null); _flag++; if (_flag == 5) _timer.Change(5000,500); else if (_flag == 10) _timer.dispose(); } } } |
以下是代码片段: public partial class MainPage : UserControl { dispatcherTimer timer; public MainPage() { InitializeComponent(); timer = new dispatcherTimer(); timer.Tick += (s,e) => { //每隔1000毫秒发生一次 //修改UI线程中的对象 this.TextBlock1.Text = DateTime.Now.ToString(); }; timer.Interval = TimeSpan.FromMilliseconds(1000); timer.Start(); } } |
以下是代码片段: public class BackgrounDWorker { // 事件声明 public event DoWorkEventHandler DoWork; public event ProgressChangedEventHandler ProgressChanged; public event RunWorkerCompletedEventHandler RunWorkerCompleted; // 方法声明 public BackgrounDWorker(); public voID CancelAsync(); protected virtual voID OnDoWork(DoWorkEventArgs e); protected virtual voID OnProgressChanged(ProgressChangedEventArgs e); protected virtual voID OnRunWorkerCompleted(RunWorkerCompletedEventArgs e); public voID ReportProgress(int percentProgress); public voID ReportProgress(int percentProgress,object userState); public voID RunWorkerAsync(); public voID RunWorkerAsync(object argument); // 属性定义 public bool CancellationPending { get; } public bool IsBusy { get; } public bool WorkerReportsProgress { get; set; } public bool WorkerSupportsCancellation { get; set; } } |
以下是代码片段: namespace SilverlightmultiThread { public partial class BackgrounDWorkerTestPage : Page { System.ComponentModel.BackgrounDWorker _backgrounDWorker; public BackgrounDWorkerTestPage() { InitializeComponent(); _backgrounDWorker = new System.ComponentModel.BackgrounDWorker(); _backgrounDWorker.WorkerSupportsCancellation = true; _backgrounDWorker.WorkerReportsProgress = true; _backgrounDWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(_backgrounDWorker_ProgressChanged); _backgrounDWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(_backgrounDWorker_DoWork); _backgrounDWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(_backgrounDWorker_RunWorkerCompleted); } private voID btnStart_Click(object sender,RoutedEventArgs e) { if (!_backgrounDWorker.IsBusy) _backgrounDWorker.RunWorkerAsync("Need Parameter!"); } private voID btnCancel_Click(object sender,RoutedEventArgs e) { if (_backgrounDWorker.WorkerSupportsCancellation) _backgrounDWorker.CancelAsync(); } voID _backgrounDWorker_DoWork(object sender,System.ComponentModel.DoWorkEventArgs e) { for (int i = 0; i < 10; i++) { if ((_backgrounDWorker.CancellationPending == true)) { e.Cancel = true; break; } else { System.Threading.Thread.Sleep(1000); _backgrounDWorker.ReportProgress((i + 1) * 10,i); } } e.Result = "Complete!"; } voID _backgrounDWorker_ProgressChanged(object sender,System.ComponentModel.ProgressChangedEventArgs e) { txtProgress.Text = string.Format("Progress:{0}%; Parameter: {1}", e.Progresspercentage, e.UserState); } voID _backgrounDWorker_RunWorkerCompleted(object sender,System.ComponentModel.RunWorkerCompletedEventArgs e) { if (e.Error != null) { txtMsg.Text += e.Error.ToString() + "\r\n"; } else if (e.Cancelled) { txtMsg.Text += "Cancelled!\r\n"; } else { txtMsg.Text += e.Result.ToString() + "\r\n"; } } } } |
以上是内存溢出为你收集整理的Silverlight 4中四种多线程编程技术全部内容,希望文章能够帮你解决Silverlight 4中四种多线程编程技术所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)