1.窗体关闭之前,顺便关闭线程
2.线程执行invork之前,先判断窗体状态
2.线程执行invork之前,先判断窗体状态
解决方案 »
- 求助:对于C#中的XML语言标注的学习参考和帮助
- [基本问题求助]方法为什么是这种格式?这些参数代表什么?
- C# 怎么样自己设置安装目录啊?而不是让他默认安装路径,高手指点下吧
- 为什么csc编译后 提示未找到元数据文件 "System.DirectoryServices.dll"
- 如何获得一个字符串中某个字符出现的次数
- 再来一贴,关于C#不尽人意的控件绘制效率,没有原因吗?
- 在自己的環境下沒錯 ,可以發到服務器上就explianss就是空,有那位大師知道這樣的原因?
- 以WebService方式整合门禁系统及人事系统的人员资料范例程序代码
- vs2005完全安装,进行了快一周也没有装完,咋回事?
- DES解密的一个方法,Java的,找高手帮转!
- 一个奇怪的正则表达式
- EnumChildWindows函数怎么用?求解答
if(textBox1!=null) textBox1.Invoke(h, i);
或
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
f = false;
}
if (f) textBox1.Invoke(h, i);都不行!
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();
}
}
}
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;
}
}
}
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)
{ }
}
}
if (textBox1.InvokeRequired)
{
SetTextHander h = s => textBox1.Text = s.ToString();
textBox1.Invoke(h, i);
}第一句时窗体还没关,第3句时窗体已经关了这种异常吞掉即可,事实上这种线程异常在非调试状态(或者你也可以设置调试时屏蔽掉)就是被吞掉的
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();
}
}
}
}
}
t.IsBackground = true;//加上这句
t.Start();
你是说,即是设置t.IsBackground = true;通过单击窗体的[X]来关闭窗体,还有30%的可能出现 0# 楼的错误吗?若是这样,应该怎样修改最为合理呢?谢谢啦~~~(目前,我测试没有探测到那30%的概率)