日期:2014-05-18  浏览次数:20803 次

线程间访问控件的问题
线程间访问控件是不是要用代理?我在网上看到的方法,但是不行,不知道是不是我写错了,下面是相关代码希望大家给看看。
C# code

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;

namespace WindowsApplication3
{

    public partial class Form1 : Form
    {
        private Thread thread;
        
        public Form1()
        {
            InitializeComponent();
            //Control.CheckForIllegalCrossThreadCalls = false;
            
        }
        delegate void mydel();
        public void dele()
        {
            //Thread.Sleep(1000);
            mydel d = new mydel(fun);
            label1.Invoke(d);
            //this.Invoke(d);
        }
        public void fun()
        {
            while (true)
            {
                int x = Convert.ToInt32(label1.Text);
                x++;
                label1.Text = Convert.ToString(x);
                Thread.Sleep(1000);
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (thread.IsAlive)
                thread.Abort();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            thread = new Thread(new ThreadStart(fun));
            label1.Text = "0";
            thread.Start();
            button1.Enabled = false;
            button2.Enabled = true;
            button3.Enabled = false;
            button4.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            thread.Suspend();
            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = true;
            button4.Enabled = false;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            thread.Resume();
            button1.Enabled = false;
            button2.Enabled = true;
            button3.Enabled = false;
            button4.Enabled = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (thread.IsAlive)
            {
                thread.Abort();
                label1.Text = "";
                button1.Enabled = true;
                button2.Enabled = false;
                button3.Enabled = false;
                button3.Enabled = false;
            }
        }
    }
}




------解决方案--------------------
C# code
using System;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private ManualResetEvent _ManualResetEvent = new ManualResetEvent(true);

        public Form1()
        {
            InitializeComponent();
        }

        private Boolean _bStop = false;

        delegate void mydel();

        public void dele()
        {
            mydel d = new mydel(fun);
            label1.Invoke(d);
        }

        public void fun()
        {
            while (!_bStop)
            {
                label1.Invoke(new MethodInvoker(delegate()
                {
                    int x = Convert.ToInt32(label1.Text);
                    x++;
                    label1.Text = Convert.ToString(x);
                }));

                _ManualResetEvent.WaitOne();
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            _ManualResetEvent.Set();
            _ManualResetEvent.Close();
            _ManualResetEvent = null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _bStop = false;

            Thread thread = new Thread(new ThreadStart(fun));
            label1.Text = "0";