C# 带参数线程的一个简单示例

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

/// <summary>
///本程序只是线程使用的一个例子,所以没有加入异常捕获语句
/// 关键要理解的地方
/// 委托:delegate
/// 调用:invoke:
/// 带参数的线程:ParameterizedThreadStart
/// </summary>
namespace csharp_thread
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ///禁用线程安全,这是1.0使用的方法,为了避免以后出现兼容性问题不应该设置为false
            ///CheckForIllegalCrossThreadCalls = false;
        }

        Thread test_thread;

        /// <summary>
        ///声明委托,他的参数必须和被委托的函数一致
        /// </summary>
        delegate void delegate_set_textbox(TextBox delegate_textbox,int textbox_value);

        /// <summary>
        ///设置文本框的值
        /// </summary>
        private void set_textbox_value(TextBox textbox,int value)
        {
            textbox.Text = value.ToString();
        }
        /// <summary>
        /// 使用委托和invoke,这个将是线程要做的事情
        /// </summary>
        private void thread_work(object arg)
        {
            int i;

            if(textBox1.InvokeRequired)
            {
                for (i = 1; i < 1000000; i++)
                    {
                        ///Invoke同步,
                        ///Invoke(new delegate_set_textbox(set_textbox_value), arg, i);
                        ///BeginInvoke异步,总是使用UI线程
                        BeginInvoke(new delegate_set_textbox(set_textbox_value), arg, i);
                        Thread.Sleep(1);
                    }         
            }
            else
            {
                for (i = 1; i < 100; i++)
                    {
                        set_textbox_value(textBox1,i);
                        Thread.Sleep(1000);
                    }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {   ///使用带参数的线程
            test_thread = new Thread(new ParameterizedThreadStart(thread_work));
            test_thread.Start(textBox1);
            label1.Text = test_thread.ThreadState.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            test_thread.Suspend();
            label1.Text = test_thread.ThreadState.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            test_thread.Resume();
            label1.Text = test_thread.ThreadState.ToString();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            test_thread.Abort();
            label1.Text = test_thread.ThreadState.ToString();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            test_thread.Abort();
            ///label1.Text = test_thread.ThreadState.ToString();
        }
 
    }
}

阅读更多
个人分类: C#
想对作者说点什么? 我来说一句

C#简单的多线程实例

线程

hdhai9451 hdhai9451

2016-08-15 23:36:17

阅读数:360

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭