ITPub博客

首页 > Linux操作系统 > Linux操作系统 > delegate Thread 的简单使用

delegate Thread 的简单使用

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-01-14 16:38:40 1 删除 编辑

using System;
using System.Windows.Forms;
using System.Threading;

namespace _Delegate
{
  
    delegate void GetDelegate();
    delegate void DelegateStart();
    delegate void TimeSprogressStep(int index);
    delegate void DelegateFinished(string Message);
    public partial class Form1 : Form
    {
        Thread aThread;
        DelegateStart delegatestart;
        TimeSprogressStep timesprogressstep;
        DelegateFinished delegatefinished;
        static int icount = 0;
        int sum = 0;

        public Form1()
        {
            InitializeComponent();

            delegatestart = new DelegateStart(this.Start);
            timesprogressstep = new TimeSprogressStep(this.TimeSprogresStep);
            delegatefinished = new DelegateFinished(this.DelegateFinished);
        }
      
        private void btnThread(object sender, EventArgs e)
        {
            aThread = new Thread(new ThreadStart(this.thread_Ms));
            aThread.IsBackground = true;
            aThread.Start();          
        }

        private void Start()
        {
            this.lblcontent.Text = "";
            sum = 1000;
            this.timeprogressBar.Maximum = sum;
            this.button2.Enabled = false;
        }
        private void TimeSprogresStep(int index)
        {
            this.timeprogressBar.PerformStep();           
            this.lblcontent.Text = string.Format("Status:{0}",index);
        }

        private void DelegateFinished(string mess)
        {          
            this.button2.Enabled = true;
            this.timeprogressBar.Value = 0;
        }
        protected void thread_Ms()
        {
            icount = 0;
            this.Invoke(delegatestart);
                 
            while (icount < sum)
            {                               
                timeprogressBar.Invoke(this.timesprogressstep, new object[] { icount});
                Application.DoEvents();                
                icount++;
            }

            this.Invoke(this.delegatefinished, new object[] { "全部完成!"});
        }

        private void btnDelegate(object sender, EventArgs e)
        {
            GetDelegate mDelegate = new GetDelegate(this.thread_Ms);
            mDelegate();
        }
    }
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-539613/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5327135