菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
359
0

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

原创
05/13 14:22
阅读数 74017
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); 
        }

    }
}

 其中,Student类的定义如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program
{
    public class Student
    {
        public int ID;
        public string Name;
        public int Age;
    }
}

 

发表评论

0/200
359 点赞
0 评论
收藏
为你推荐 换一批