zoukankan      html  css  js  c++  java
  • WinForm 中使用 Action 子线程对主线程 控制进行访问

     1 /// <summary>
     2 /// 开启新线程执行
     3 /// </summary>
     4 /// <param name="sender"></param>
     5 /// <param name="e"></param>
     6 private void button1_Click(object sender, EventArgs e)
     7 {
     8 Task.Factory.StartNew(() =>
     9 {
    10 Run(setLog);
    11 });
    12 }
    13 
    14 /// <summary>
    15 /// 线程工作内容
    16 /// </summary>
    17 /// <param name="act"></param>
    18 void Run(Action<string, DateTime> act)
    19 {
    20 for (int i = 0; i < 99; i++)
    21 {
    22 Thread.Sleep(1000);
    23 
    24 //方法一
    25 Invoke(act, i.ToString(), DateTime.Now.AddDays(i));
    26 
    27 //方法二
    28 var newAct = new Action<string, DateTime>(setLog);
    29 Invoke(newAct, i.ToString(), DateTime.Now.AddDays(i));
    30 }
    31 }
    32 
    33 /// <summary>
    34 /// 设置文本框内容
    35 /// </summary>
    36 /// <param name="msg"></param>
    37 /// <param name="dts"></param>
    38 void setLog(string msg, DateTime dts)
    39 {
    40 this.richTextBox1.AppendText(string.Format("当前时间增加 {0} 天后结果为:{1}{2}", msg, dts, Environment.NewLine));
    41 }

    WinForm 中使用 Action 子线程对主线程 控制进行访问

  • 相关阅读:
    KVCKVO
    音频
    静态库
    百度地图API
    CALayer
    触摸事件
    iOS中打电话、打开网址、发邮件、发短信等
    NSURLSession网络接口
    Quartz2D常见图形的绘制:线条、多边形、圆
    通知中心(NSNotificationCenter)
  • 原文地址:https://www.cnblogs.com/intotf/p/7806749.html
Copyright © 2011-2022 走看看