zoukankan      html  css  js  c++  java
  • DelegateAndAsync

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;

    namespace DelegateAndAsync
    {
     /// <summary>
     /// Form1 的摘要说明。
     /// </summary>
     public class frmMain : System.Windows.Forms.Form
     {

      public delegate DateTime WorkHandler(int sleepMillisecond);

      private System.Windows.Forms.Button CmdStart;
      private System.Windows.Forms.RichTextBox RT001;
      private System.Windows.Forms.ProgressBar PB001;
      /// <summary>
      /// 必需的设计器变量。
      /// </summary>
      private System.ComponentModel.Container components = null;

      public frmMain()
      {
       //
       // Windows 窗体设计器支持所必需的
       //
       InitializeComponent();

       //
       // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
       //
      }

      /// <summary>
      /// 清理所有正在使用的资源。
      /// </summary>
      protected override void Dispose( bool disposing )
      {
       if( disposing )
       {
        if (components != null)
        {
         components.Dispose();
        }
       }
       base.Dispose( disposing );
      }

      #region Windows 窗体设计器生成的代码
      /// <summary>
      /// 设计器支持所需的方法 - 不要使用代码编辑器修改
      /// 此方法的内容。
      /// </summary>
      private void InitializeComponent()
      {
       this.CmdStart = new System.Windows.Forms.Button();
       this.RT001 = new System.Windows.Forms.RichTextBox();
       this.PB001 = new System.Windows.Forms.ProgressBar();
       this.SuspendLayout();
       //
       // CmdStart
       //
       this.CmdStart.Location = new System.Drawing.Point(104, 232);
       this.CmdStart.Name = "CmdStart";
       this.CmdStart.TabIndex = 0;
       this.CmdStart.Text = "Start";
       this.CmdStart.Click += new System.EventHandler(this.CmdStart_Click);
       //
       // RT001
       //
       this.RT001.Location = new System.Drawing.Point(8, 8);
       this.RT001.Name = "RT001";
       this.RT001.Size = new System.Drawing.Size(272, 96);
       this.RT001.TabIndex = 1;
       this.RT001.Text = "";
       //
       // PB001
       //
       this.PB001.Location = new System.Drawing.Point(8, 152);
       this.PB001.Name = "PB001";
       this.PB001.Size = new System.Drawing.Size(272, 23);
       this.PB001.TabIndex = 2;
       //
       // frmMain
       //
       this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
       this.ClientSize = new System.Drawing.Size(292, 273);
       this.Controls.Add(this.PB001);
       this.Controls.Add(this.RT001);
       this.Controls.Add(this.CmdStart);
       this.Name = "frmMain";
       this.Text = "Test";
       this.ResumeLayout(false);

      }
      #endregion

      /// <summary>
      /// 应用程序的主入口点。
      /// </summary>
      [STAThread]
      static void Main()
      {
       Application.Run(new frmMain());
      }

      private void CmdStart_Click(object sender, System.EventArgs e)
      {
       MessageBox.Show("Start");
       WorkHandler wHandle = new WorkHandler(this.StartWork);
       wHandle.BeginInvoke(10000,new AsyncCallback(this.CompleteWork),wHandle);
       
      }

      /// <summary>
      ///
      /// </summary>
      /// <param name="sleepMillisecond"></param>
      /// <returns></returns>
      private DateTime StartWork(int sleepMillisecond)
      {
       this.CmdStart.Enabled = false;
       int val = 0;
       while(true)
       {
        val += 10/(sleepMillisecond/1000);
        System.Threading.Thread.Sleep(1000/(sleepMillisecond/1000));
        this.PB001.Value = val;
        if(val == 100 || val > 100 )
         break;

       }
       return DateTime.Now;
      
      }

      /// <summary>
      /// 当StartWork完成后该做的任务
      /// </summary>
      /// <param name="ar"></param>
      private void CompleteWork(IAsyncResult ar)
      {
       try
       {
        WorkHandler WH=(WorkHandler)ar.AsyncState;
        DateTime dt = WH.EndInvoke(ar);   
        MessageBox.Show(this,dt.ToString());
        this.CmdStart.Enabled = true;
       }
       catch(Exception e)
       {
        Console.Write(e);
       }
      }

      private void CompleteWork()
      {
       
      }

     }
    }

  • 相关阅读:
    poj3278 Catch That Cow
    poj2251 Dungeon Master
    poj1321 棋盘问题
    poj3083 Children of the Candy Cor
    jvm基础知识—垃圾回收机制
    jvm基础知识1
    java面试基础必备
    java soket通信总结 bio nio aio的区别和总结
    java scoket aIO 通信
    java scoket Blocking 阻塞IO socket通信四
  • 原文地址:https://www.cnblogs.com/sinxsoft/p/369326.html
Copyright © 2011-2022 走看看