请选择 进入手机版 | 继续访问电脑版

马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

异步加载树节点

  [复制链接]
查看3597 | 回复17 | 2019-12-26 09:06:18 | 显示全部楼层 |阅读模式
  1. 1 using System;
  2. 2 using System.Collections.Generic;
  3. 3 using System.ComponentModel;
  4. 4 using System.Data;
  5. 5 using System.Drawing;
  6. 6 using System.Text;
  7. 7 using System.Threading;
  8. 8 using System.Windows.Forms;
  9. 9
  10. 10 namespace FormCallBackTest
  11. 11 {
  12. 12 public partial class Form1 : Form
  13. 13 {
  14. 14 //声明委托已实现回调机制
  15. 15 private delegate void CallBackTest();
  16. 16 private CallBackTest callBackTest;
  17. 17
  18. 18 public delegate void DelegateDone();//界说一个委托
  19. 19 public DelegateDone GetDelegateHandler; //声明委托
  20. 20
  21. 21 public Form1()
  22. 22 {
  23. 23 InitializeComponent();
  24. 24
  25. 25 //初始化回调
  26. 26 GetDelegateHandler = new DelegateDone(ThdFunc); //绑定须要回调的函数
  27. 27 }
  28. 28 private void button1_Click(object sender, EventArgs e)
  29. 29 {
  30. 30 Thread th1 = new Thread(new ThreadStart(DoWork));//创建线程
  31. 31 th1.IsBackground = true;//背景线程
  32. 32 th1.Start();//启动线程
  33. 33 }
  34. 34
  35. 35 private void ThdFunc()
  36. 36 {
  37. 37 List<string> listTest = new List<string>();
  38. 38
  39. 39 GetData(ref listTest);//获取数据
  40. 40 InitTreeInfo(listTest);//构建立节点
  41. 41 }
  42. 42 private void GetData(ref List<string> listTest)
  43. 43 {
  44. 44 listTest.Clear();
  45. 45 for (int i = 0; i < 1000000; i++)
  46. 46 {
  47. 47 listTest.Add("测试数据" + i);
  48. 48 }
  49. 49 }
  50. 50 private void InitTreeInfo(List<string> listTest)
  51. 51 {
  52. 52 for (int i = 0; i < listTest.Count; i++)
  53. 53 {
  54. 54 TreeNode SubNode = new TreeNode();
  55. 55 SubNode.Name = Convert.ToString(i + 1);
  56. 56 SubNode.Text = listTest[i];
  57. 57 this.Invoke((MethodInvoker)delegate()
  58. 58 {
  59. 59 treeView1.Nodes.Add(SubNode);
  60. 60 });//防止跨线程
  61. 61 }
  62. 62 }
  63. 63 void DoWork()
  64. 64 {
  65. 65 callBackTest = new CallBackTest(GetDelegateHandler);
  66. 66 CallBack(callBackTest);
  67. 67 }
  68. 68 //使用委托
  69. 69 private void CallBack(CallBackTest Delegate)
  70. 70 {
  71. 71 Delegate();
  72. 72 }
  73. 73
  74. 74 }
  75. 75 }<br /> 源码下载地点:https://files-cdn.cnblogs.com/files/yc1224/%E5%BC%82%E6%AD%A5%E5%8A%A0%E8%BD%BD%E6%A0%91%E8%8A%82%E7%82%B9.zip
复制代码

C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则