1.窗体关闭之前,顺便关闭线程
2.线程执行invork之前,先判断窗体状态

解决方案 »

  1.   

    不知道怎么来判断:if (!this.IsDisposed) textBox1.Invoke(h, i);
    if(textBox1!=null) textBox1.Invoke(h, i);

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        f = false;            
    }
    if (f) textBox1.Invoke(h, i);都不行!
      

  2.   

    关闭事件中判断IsAlive,然后用Thread.Abort()终止线程
      

  3.   

    谢谢,这样是可以了,但Thread t却不在是一个局部变量了。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.Text.RegularExpressions;
    using System.IO;
    using System.IO.MemoryMappedFiles;
    using System.Threading;namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            private delegate void SetTextHander(int num);//带参数
            Thread t=null;
            public Form1()
            {
                InitializeComponent();
            }        private void button3_Click(object sender, EventArgs e)
            {
                t = new Thread(DoWork);
                t.Start();
            }        void DoWork()
            {
                for (int i = 1; i < 888888888; ++i)
                {
                    if (textBox1.InvokeRequired)
                    {
                        SetTextHander h = s => textBox1.Text = s.ToString();
                        textBox1.Invoke(h, i);
                    }
                    else
                    {
                        textBox1.Text = i.ToString();
                    }
                    
                }
            }        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if(t.IsAlive)t.Abort();
            }
        }
    }
      

  4.   

    其实我想如下面这样进行控制,但却无法正常结束线程啊~~~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.Text.RegularExpressions;
    using System.IO;
    using System.IO.MemoryMappedFiles;
    using System.Threading;namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            private delegate void SetTextHander(int num);//带参数
            bool isRunning = true;//线程运行标记
            public Form1()
            {
                InitializeComponent();
            }        private void button3_Click(object sender, EventArgs e)
            {
                Thread t = new Thread(DoWork);
                t.Start();
            }        void DoWork()
            {
                for (int i = 1; isRunning && i < 888888888; ++i)
                {
                    if (textBox1.InvokeRequired)
                    {
                        SetTextHander h = s => textBox1.Text = s.ToString();
                        textBox1.Invoke(h, i);
                    }
                    else
                    {
                        textBox1.Text = i.ToString();
                    }
                    
                }
            }        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                isRunning = false;
            }
        }
    }
      

  5.   

    把线程的定义放外面,然后在窗体的关闭事件中写一句 t.Abort()。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 WindowsFormsApplication7
    {
        public partial class Form1 : Form
        {
            private delegate void SetTextHander(int num);        public Form1()
            {
                InitializeComponent();
            }        System.Threading.Thread t = null;        private void button1_Click(object sender, EventArgs e)
            {
                t = new Thread(DoWork);
                t.Start();
            }        void DoWork()
            {
                for (int i = 1; i < 888888888; ++i)
                {
                    if (textBox1.InvokeRequired)
                    {
                        SetTextHander h = s => textBox1.Text = s.ToString();
                        textBox1.Invoke(h, i);
                    }
                    else
                    {
                        textBox1.Text = i.ToString();
                    }                Thread.Sleep(100);
                }
            }        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                t.Abort();
            }         private void Form1_Load(object sender, EventArgs e)
            {        }
        }
    }
      

  6.   

    根本原因是并发冲突
      if (textBox1.InvokeRequired)
                    {
                        SetTextHander h = s => textBox1.Text = s.ToString();
                        textBox1.Invoke(h, i);
                    }第一句时窗体还没关,第3句时窗体已经关了这种异常吞掉即可,事实上这种线程异常在非调试状态(或者你也可以设置调试时屏蔽掉)就是被吞掉的
      

  7.   

    发现这样处理最美!!!大家说呢??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.Text.RegularExpressions;
    using System.IO;
    using System.IO.MemoryMappedFiles;
    using System.Threading;namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            private delegate void SetTextHander(int num);//带参数
            public Form1()
            {
                InitializeComponent();
            }        private void button3_Click(object sender, EventArgs e)
            {
                Thread t = new Thread(DoWork);
                t.IsBackground = true;
                t.Start();
            }        void DoWork()
            {
                for (int i = 1;  i < 888888888; ++i)
                {
                    if (textBox1.InvokeRequired)
                    {
                        SetTextHander h = s => textBox1.Text = s.ToString();
                        textBox1.Invoke(h, i);
                    }
                    else
                    {
                        textBox1.Text = i.ToString();
                    }                
                }
            }
        }
    }
      

  8.   

    Thread t = new Thread(DoWork);
                t.IsBackground = true;//加上这句
                t.Start();
      

  9.   


    你是说,即是设置t.IsBackground = true;通过单击窗体的[X]来关闭窗体,还有30%的可能出现 0# 楼的错误吗?若是这样,应该怎样修改最为合理呢?谢谢啦~~~(目前,我测试没有探测到那30%的概率)
      

  10.   

    虽然已结贴,但我仍会关注该贴,若sp1234 看到此贴,请对最后的表述说明一下,O(∩_∩)O谢谢~~~