zoukankan      html  css  js  c++  java
  • c# this.Invoke的定义及用法(个人理解的用法)

     C# this.invoke()作用

    Invoke()的作用是:在应用程序的主线程上执行指定的委托。一般应用:在辅助线程中修改UI线程( 主线程 )中对象的属性时,调用this.Invoke();

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9 using System.Threading;
    10 namespace Invoke_WindowsForm
    11 {
    12     public partial class Form1 : Form
    13     {
    14         public Form1()
    15         {
    16             InitializeComponent();
    17         }
    18 
    19         private void Form1_Load(object sender, EventArgs e)
    20         {
    21             Thread d = new Thread(new ThreadStart(this.DoSomething));
    22             d.Start();
    23         }
    24         private void DoSomething()
    25         {
    26             for (int i = 0; i < 100; i++)
    27             {
    28                 this.Invoke(new MethodInvoker(() => {
    29                     Thread.Sleep(100);
    30                     this.label1.Text = i.ToString();
    31                 }));
    32             }
    33         }
    34     }
    35 }

    以上代码就是动态改变UI上空间值得方法 主线程会在UI线程和辅助线程之间相互转换 

  • 相关阅读:
    C#之流程控制
    UML画图总结以及浅谈UNL九种图
    UML视频总结
    英语总结
    UML coming
    那天我把“小四”拆了
    first 关于文档(总结)
    机房收费需求分析文档
    梦开始的地方
    WebRTC 开发实践:为什么你需要 SFU 服务器
  • 原文地址:https://www.cnblogs.com/ganler1988/p/2754121.html
Copyright © 2011-2022 走看看