C# 多线程 与 委托

简单的多线程:

        protected void Page_Load(object sender, EventArgs e)
        {
            ParameterizedThreadStart _ParameterizedThreadStart = new ParameterizedThreadStart(MyMethod);
            Thread t = new Thread(_ParameterizedThreadStart);
            object o = "sdfsf";
            t.Start(o);
        }


        private void MyMethod(object o)
        {
            //执行任务
}

使用委托的多线程:

public partial class Demo : Form
    {
        public Demo()
        {
            InitializeComponent();
        }

        //定义委托
        delegate void MyDelegate(string str);
        //声明委托
        MyDelegate _MyDelegate;
        //声明线程
        private Thread t;
        private void Demo_Load(object sender, EventArgs e)
        {
            //给函数指定委托
            _MyDelegate = new MyDelegate(SetLblTxt);
        }

        //执行委托
        private void Run()
        {
            while (true)
            {
                lblShow.Invoke(_MyDelegate, new object[] { Guid.NewGuid().ToString() });
                Thread.Sleep(100);
            }
        }

        //设定lable的text
        private void SetLblTxt(string o)
        {
            this.lblShow.Text = o.ToString();
        }

        private void btnBegin_Click(object sender, EventArgs e)
        {
            t = new Thread(new ThreadStart(Run));
            t.Start();
        }

        private void Demo_FormClosing(object sender, FormClosingEventArgs e)
        {
            //终止线程
            if (t.IsAlive) 
            {
                t.Abort(); 
            }
        }
    }

 有返回值的多线程:

//定义委托
        delegate string MyDelegate(string str);
        //声明委托
        MyDelegate _MyDelegate;
        //声明线程
        private Thread t;
        private void Demo_Load(object sender, EventArgs e)
        {
            //给函数指定委托
            _MyDelegate = new MyDelegate(SetLblTxt);
        }

        //执行委托
        private void Run()
        {
            object o = new object();
            while (true)
            {
                lock (lblShow)
                {
                    //lblShow.Invoke(_MyDelegate, new object[] { Guid.NewGuid().ToString() }); //其实是同步调用
                    IAsyncResult result = lblShow.BeginInvoke(_MyDelegate, new object[] { Guid.NewGuid().ToString() }); //异步调用
                    MessageBox.Show(lblShow.EndInvoke(result).ToString());  //获得返回值
                    Thread.Sleep(1000);
                }
            }
        }

        //设定lable的text
        private string SetLblTxt(string o)
        {
            this.lblShow.Text = o.ToString();
            return o.ToString();
        }

        private void btnBegin_Click(object sender, EventArgs e)
        {
            t = new Thread(new ThreadStart(Run));
            t.Start();
        }

        private void Demo_FormClosing(object sender, FormClosingEventArgs e)
        {
            //终止线程
            if (t == null) return;
            if (t.IsAlive) 
            {
                t.Abort(); 
            }
        }

 

posted @ 2013-02-27 10:00  划破黑夜  阅读(6918)  评论(0编辑  收藏  举报