zoukankan      html  css  js  c++  java
  • 跨线程程访问Win form 控件

    http://blog.csdn.net/farawayplace613/article/details/4849632

        public static class CrossThreadUtility
        {
            public static void InvokeControlAction<t>(t cont, Action<t> action) where t : System.Windows.Forms.Control
            {
                if (cont.InvokeRequired)
                {
                    cont.Invoke(new Action<t, Action<t>>(InvokeControlAction), new object[] { cont, action });
                }
                else
                {
                    action(cont);
                }
            }
        }
    

      

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace CrossThreadUtil
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                //Initialize and start the timer
                System.Timers.Timer t = new System.Timers.Timer(1000);
                t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
                t.Start();
            }
    
            void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                           CrossThreadUtility.InvokeControlAction<Label>(lblTime, lbl => lbl.Text = String.Format("The current time is: {0}", DateTime.Now.ToString("h:mm:ss tt")));
            }
        }
    }
    
    //Form1.Designer.cs
    
     
    
    namespace CrossThreadUtil
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.lblTime = new System.Windows.Forms.Label();
                this.SuspendLayout();
                // 
                // lblTime
                // 
                this.lblTime.AutoSize = true;
                this.lblTime.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.lblTime.Location = new System.Drawing.Point(92, 133);
                this.lblTime.Name = "lblTime";
                this.lblTime.Size = new System.Drawing.Size(0, 24);
                this.lblTime.TabIndex = 0;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(484, 324);
                this.Controls.Add(this.lblTime);
                this.Name = "Form1";
                this.Text = "Clock";
                this.Load += new System.EventHandler(this.Form1_Load);
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.Label lblTime;
        }
    }
    

      

  • 相关阅读:
    POJ 1789:Truck History
    POJ 1258:Agri-Net Prim最小生成树模板题
    POJ 1837:Balance 天平DP。。。
    杭电1754--I Hate It(线段树)
    Poj3259--Wormholes(Spfa 判负环)
    杭电1068--Girls and Boys(二分图最大独立集)
    杭电1010--Tempter of the Bone(Dfs+剪枝)
    杭电2647--Reward(反向拓扑)
    杭电1083--Courses(二分图匹配)
    杭电2063--过山车(二分匹配)
  • 原文地址:https://www.cnblogs.com/YYi_H/p/2168648.html
Copyright © 2011-2022 走看看