zoukankan      html  css  js  c++  java
  • C#中工作线程处理完数据后将处理结果返回给UI主线程通知主线程操作界面

    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;
        }
    }
  • 相关阅读:
    Java 8 forEach简单例子
    Intellij IDEA 使用
    java中的sleep()和wait()的区别
    MySQL问答整理
    java 正则例子
    如何在两个线程之间共享数据
    蜻蜓FM涉嫌诈骗投资人和广告主源代码剖析
    玩DNF开启NVIDIA独显的方法
    WSAEventSelect模型编程 详解
    签名时加密失败 --“对程序集签名时出错
  • 原文地址:https://www.cnblogs.com/rainbow70626/p/13649289.html
Copyright © 2011-2022 走看看