silverlight多线程学习(一)Thread

Thread线程

 

注意要点

一、不能直接访问UI线程。也就是和UI界面同步,一同步也就是操作和更新UI界面。这时候UI线程自然就会阻塞。这个在DispatcherTimer定时器是可以直接访问更新UI界面的线程。在DispatcherTimer线程里会有介绍。

二、如果需要跟新访问UI界面元素稍稍代码上优点小繁琐。

 

silverlight+WCF开发,由于WCF默认方式为异步。虽然能实现WCF同步。但是,是以阻塞线程达到的。线程阻塞用户体验度就降低了。

实例中设置了线程的后台模式,可以把它简单的理解成。让微软的内存回收机制来管理。

  this.Dispatcher.BeginInvoke用来异步操作UI, 使用MVVM模式就基本没必要操作UI了。

Lock为内存加锁,防止多个线程同时操作一个变量产生BUG。写多线程个人认为最好直接加锁。以免留下隐患。

MEF和多线程配合还是蛮好玩的。

实例代码为VS2010+silverlight4

下面是简单的线程,更新txt的时间

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Threading;

 

namespace 多线程

{

    public partial class MainPage : UserControl

    {

        string strTime = null;

 

        public MainPage()

        {

            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

        }

 

        void MainPage_Loaded(object sender, RoutedEventArgs e)

        {

            ThreadTest();

        }

 

        /// <summary>

        /// 线程测试

        /// </summary>

        private void ThreadTest()

        {

            //定义线程¨

            Thread myFirstThread = new Thread(new ThreadStart(GetTime));

            //定义为后台模式

            myFirstThread.IsBackground = true;

            //启动线程

            myFirstThread.Start();

        }

 

        private void GetTime()

        {

            while (true)

            {

                //线程异步的委托更新UI用此种写法,不更新就不用了

                this.Dispatcher.BeginInvoke(() =>

                {

                    strTime = DateTime.Now.ToString();

                    if (!string.IsNullOrEmpty(strTime))

                    {

                        //内存加锁

                        lock (this)

                        {

 

                            this.textBox1.Text = strTime;

                        }

                    }

 

                });

                //设置休眠时间

                Thread.Sleep(10000);

 

            }

 

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            ThreadTest();

        }

    }

}

 

  但是上面这种写法不能被封装,主要是Dispatcher的获取问题。 Deployment.Current.Dispatcher就能获取了。

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;

 

namespace 多线程2
{
    public class MyThreading
    {
        string strTime = null;
        TextBox txt1 = null;

        public MyThreading(TextBox txt)
        {
            txt1 = txt;
            ThreadTest();
        }
        /// <summary>
        /// 线程测试
        /// </summary>
        private void ThreadTest()
        {
            //定义线程
            Thread myFirstThread = new Thread(new ThreadStart(GetTime));
            //定义为后台模式
            myFirstThread.IsBackground = true;
            //启动线程
            myFirstThread.Start();
        }

        private void GetTime()
        {
            while (true)
            {
                //线程异步的委托,更新UI用此种写法,不更新就不用了。
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    strTime = DateTime.Now.ToString();
                    if (!string.IsNullOrEmpty(strTime))
                    {
                        //内存加锁
                        lock (this)
                        {

                            txt1.Text = strTime;
                        }
                    }

                });
                //设置休眠时间
                Thread.Sleep(1000);

            }

        }
    }
}

原文地址:https://www.cnblogs.com/Areas/p/2196787.html