首页 > 编程语言 > 详细

C#中工作线程处理完数据后将处理结果返回给UI主线程通知主线程操作界面

时间:2020-09-11 10:07:55      阅读:169      评论:0      收藏:0      [点我收藏+]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading; //线程类:暂停函数

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            //方法1:
            //Action<Student> callback = ((Student st) => { Console.WriteLine(st.Name); });//lambda 表达式

            //方法3:
            Action<Student> callback = ShowName;

            Thread th = new Thread(Fun);
            th.IsBackground = true;
            th.Start(callback);
            Console.ReadKey();
        }

        private static void Fun(object obj)
        {
            for (int i = 1; i <= 10; i++)
            {
                Console.WriteLine("子线程循环操作第 {0} 次", i);
                Thread.Sleep(500);
            }

            Action<Student> callback = obj as Action<Student>;

            Student st = new Student();
            st.ID = 1;
            st.Name = "Long";
            st.Age = 20;

            callback(st);
        }

        //方法2:上面的Lambda表达式也可以回城匿名函数
        private static Action<Student> callback = delegate(Student st) { Console.WriteLine(st.Name); };

        //方法3:
        private static void ShowName(Student st)
        {
            Console.WriteLine(st.Name); 
        }

    }
}

 

C#中工作线程处理完数据后将处理结果返回给UI主线程通知主线程操作界面

原文:https://www.cnblogs.com/rainbow70626/p/13649289.html

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