首页 > 编程语言 > 详细

多线程学习之AsyncOperation实现线程间交互

时间:2014-03-19 21:41:30      阅读:437      评论:0      收藏:0      [点我收藏+]

1、首先我们要实现如下图的效果:                                                         

  a、主线程A运行方法段1时创建子线程B

  b、然后子线程B执行方法段2

  c、执行完后通知主线程A执行方法段3

bubuko.com,布布扣

 

 

2、实现代码如下:                     

bubuko.com,布布扣
        public Form1()
        {
            InitializeComponent();
        }

        public void Method1()
        {
            //给主线程取个名字
            if (Thread.CurrentThread.Name == null)
                Thread.CurrentThread.Name = "main";

            //获取主线程上下文
            asyncOperation = AsyncOperationManager.CreateOperation(null);

            //创建一个线程并执行方法2
            ThreadStart ts = new ThreadStart(Method2);
            Thread t = new Thread(ts);
            t.Name = "work";
            t.Start();
        }

        public void Method2()
        {
            MessageBox.Show("当前线程:" + Thread.CurrentThread.Name);

            //模拟干了一件3秒耗时的事情
            Thread.Sleep(3000);

            //通知主线程我事情干好了,你可以执行方法3了
            if (asyncOperation != null)
                asyncOperation.Post(new SendOrPostCallback(Method3), null);

            //结束上下文的生存期
            asyncOperation.OperationCompleted();
        }

        public void Method3(object data)
        {
            MessageBox.Show("当前线程:" + Thread.CurrentThread.Name);
        }

        private AsyncOperation asyncOperation;
        private void button1_Click(object sender, EventArgs e)
        {
            Method1();
        } 
bubuko.com,布布扣

 

3、要点记录                        

1、AsyncOperation 一般通过 AsyncOperationManager 对象获取

asyncOperation = AsyncOperationManager.CreateOperation(null);

2、在System.Windows.Forms.Form的UI线程存在对应的上下文AsyncOperation,在Conosol控制台程序的主线程不存在上下文AsyncOperation

     给个例子一看就清楚了

bubuko.com,布布扣
        static AsyncOperation asyncOperation;

        static void Main(string[] args)
        {
            Console.WriteLine("我是主线程:" + Thread.CurrentThread.ManagedThreadId);
            Method1();
            Console.ReadKey();
        }

        static void Method1()
        {

            //获取主线程上下文
            asyncOperation = AsyncOperationManager.CreateOperation(null);

            //创建一个线程
            ThreadStart ts = new ThreadStart(Method2);
            Thread t = new Thread(ts);
            t.Start();
        }

        static void Method2()
        {
            Console.WriteLine("我是子线程:" + Thread.CurrentThread.ManagedThreadId);

            //模拟干了一件3秒耗时的事情
            Thread.Sleep(3000);

            //通知主线程事情干好了,其实这里创建了一个新线程
            if (asyncOperation != null)
                asyncOperation.Post(new SendOrPostCallback(Method3), null);

            //结束上下文的生存期
            asyncOperation.OperationCompleted();
        }

        static void Method3(object data)
        {
            Console.WriteLine("我是上下文创建的线程:" + Thread.CurrentThread.ManagedThreadId);
        }
bubuko.com,布布扣

 

 

多线程学习之AsyncOperation实现线程间交互,布布扣,bubuko.com

多线程学习之AsyncOperation实现线程间交互

原文:http://www.cnblogs.com/xm_cpppp/p/3612055.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!