是这样的  我用Timer设置5秒执行一个方法  方法里面会每次new线程 然后执行我需要执行的方法。
现在,我界面放置一个按钮,想在按钮事件里面停止当前的线程,因为每次是在timer事件里面new的线程,所以怎么能让他找到并停止那个线程。 

解决方案 »

  1.   

    本帖最后由 bdmh 于 2012-11-12 11:39:27 编辑
      

  2.   

    Timer本来就是多线程的,不知道你注意没有。
      

  3.   

    首先你要知道你的timer做什么事情,执行间隔够不够!执行间隔够了的话直接timer内部处理就好啊!
      

  4.   

    方法A()  每隔一段时间会自动执行,用来刷新界面数据  然后  现在有N多按钮 我想通过点击按钮让自动执行的方法A()停止或者直接不运行(并不知道当前执行到哪一步),反正不能往下执行了  然后在按钮事件完成之后在重新执行,不用接着刚才的,我设置了timer测试多的,点击后禁用timer,不过不行,还是把当前方法执行完了之后才不自信timer事件的。  我贴代码你们看看,看有什么方法可以解决;
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;namespace CDCurrentControl.UI
    {
        public partial class sdf : Form
        {
            public sdf()
            {
                InitializeComponent();        }        int i = 0;        void aaaa()
            {
                timer1.Interval = 1000;
                Thread.Sleep(2000);
                i++;
                Thread.Sleep(2000);
                i++;            this.label1.Text = i.ToString();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                this.timer1.Enabled = false;
                ccca = false;
                //this.timer1.Enabled = true;
            }        private void sdf_Load(object sender, EventArgs e)
            {
            }
            bool ccca = true;
            private void timer1_Tick(object sender, EventArgs e)
            {
                if (ccca == true)
                {
                    aaaa();
                }
                
            }    }
    }
      

  6.   

    timer1_Tick中没有创建线程呀!?只是在ccca == true时调用了aaaa()这个函数而已...
      

  7.   

    现在不是说线程的问题了。。方法A()  每隔一段时间会自动执行,用来刷新界面数据  然后  现在有N多按钮 我想通过点击按钮让自动执行的方法A()停止或者直接不运行(并不知道当前执行到哪一步),反正不能往下执行了  然后在按钮事件完成之后在重新执行,不用接着刚才的,我设置了timer测试多的,点击后禁用timer,不过不行,还是把当前方法执行完了之后才不自信timer事件的。  我想实现这个  不知道可以不  我在按钮事件中禁用timer不行。。当前的方法还是要执行完的 有什么办法能在点击后当前方法就直接不执行了
      

  8.   

    我知道直接关闭。。但是关闭的时候已经进入到aaaa那个方法了  我想点击按钮的时候不进入那个方法 。。
      

  9.   

    线程一旦开始执行,除非挂起或正常结束或abort,是不会停止的
      

  10.   

    你可以用Thread.Abort来终止执行aaa方法的线程.
      

  11.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    using System.Transactions;
    namespace CDCurrentControl.UI
    {
        public partial class sdf : Form
        {
            public sdf()
            {
                InitializeComponent();
            }
            AutoResetEvent are = new AutoResetEvent(true);        int i = 0;
            void aaaa(object ni)
            {
                do
                {                this.label1.Text = (i++).ToString();
                    Thread.Sleep(1000);
                    this.label1.Text = (i++).ToString();
                    Thread.Sleep(1000);
                    this.label1.Text = (i++).ToString();
                    Thread.Sleep(1000);
                    this.label1.Text = (i++).ToString();
                    Thread.Sleep(1000);
                    this.label1.Text = (i++).ToString();
                    Thread.Sleep(1000);
                    this.label1.Text = (i++).ToString();
                    Thread.Sleep(1000);
                    this.label1.Text = (i++).ToString();
                    
                } while (true);
            }
            private void button1_Click(object sender, EventArgs e)
            {
                wocao[0].Suspend();
                Thread.Sleep(5000);
                wocao[0].Resume();        }
            List<Thread> wocao = new List<Thread>();
            private void sdf_Load(object sender, EventArgs e)
            {
                Control.CheckForIllegalCrossThreadCalls = false;
                Thread th = new Thread(aaaa);
                th.Start();            wocao.Add(th);
            }    }
    }嗯 这样就可以了我怎么这么二呢。。