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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//创建一个委托,是为访问TextBox控件服务的。
public delegate void UpdateTxt(string msg);
//定义一个委托变量
public UpdateTxt updateTxt;
//修改TextBox值的方法。
public void UpdateTxtMethod(string msg)
{
richTextBox1.AppendText(msg + "\r\n");
richTextBox1.ScrollToCaret();
}
//此为在非创建线程中的调用方法,其实是使用TextBox的Invoke方法。
public void ThreadMethodTxt(int n)
{
this.BeginInvoke(updateTxt, "线程开始执行,执行" + n + "次,每一秒执行一次");
for (int i = 0; i < n; i++)
{
this.BeginInvoke(updateTxt, i.ToString());
//一秒 执行一次
Thread.Sleep(1000);
}
this.BeginInvoke(updateTxt, "线程结束");
}
//开启线程
private void button1_Click(object sender, EventArgs e)
{
Thread objThread = new Thread(new ThreadStart(delegate
{
ThreadMethodTxt(Convert.ToInt32(textBox1.Text.Trim()));
}));
objThread.Start();
}
private void Form1_Load_1(object sender, EventArgs e)
{
//实例化委托
updateTxt = new UpdateTxt(UpdateTxtMethod);
}
}
}
this.BeginInvoke(updateTxt, "线程结束");
updateTxt("线程结束");
updateTxt("线程结束");
qq576733600 发表于 2013-6-8 08:29
太棒了。 刚好 需要这个 ,找了好多资料,没有实现的了。
sandy1231 发表于 2013-12-11 23:22
BeginInvoke和Invoke有什么区别呢
for (int i = 0; i < 100; i++)
{
Thread red = new Thread(new ThreadStart(delegate
{
Fabu();
}));
red.Start();
}
public Form1()
{
InitializeComponent();
}
//创建一个委托,是为访问TextBox控件服务的。
public delegate void UpdateTxt(string msg);
//定义一个委托变量
public UpdateTxt updateTxt;
Thread objThread;
//修改TextBox值的方法。
public void UpdateTxtMethod(string msg)
{
richTextBox1.AppendText(msg + "\r\n");
richTextBox1.ScrollToCaret();
}
//此为在非创建线程中的调用方法,其实是使用TextBox的Invoke方法。
public void ThreadMethodTxt(int n)
{
while (!this.IsHandleCreated)
{
}
this.BeginInvoke(updateTxt, "线程开始执行,执行" + n + "次,每一秒执行一次");
for (int i = 0; i < n; i++)
{
while (!this.IsHandleCreated)
{
}
this.BeginInvoke(updateTxt, i.ToString());
//一秒 执行一次
Thread.Sleep(1000);
}
this.BeginInvoke(updateTxt, "线程结束");
}
//开启线程
private void button1_Click(object sender, EventArgs e)
{
objThread = new Thread(new ThreadStart(delegate
{
ThreadMethodTxt(Convert.ToInt32(textBox1.Text.Trim()));
}));
objThread.Start();
}
private void Form1_Load_1(object sender, EventArgs e)
{
//实例化委托
updateTxt = new UpdateTxt(UpdateTxtMethod);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
objThread.Abort();
}
wkpeng 发表于 2014-5-15 09:48
这金钱上哪赚?
Delegate.Invoke: Executes synchronously, on the same thread.
Delegate.BeginInvoke: Executes asynchronously, on a threadpool thread.
Control.Invoke: Executes on the UI thread, but calling thread waits for completion before continuing.
Control.BeginInvoke: Executes on the UI thread, and calling thread doesn't wait for completion.
我是MT 发表于 2015-1-25 00:25
这个方法用在dataGridView中貌似不行哦。还是一样的卡死界面,只能等到数据全部处理完才能不卡。
龍游天下 发表于 2015-3-12 11:52
如果提前关闭窗口,则出错:在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke。
神同步 发表于 2015-7-22 21:11
其实我喜欢在多线程的时候对主线程窗体的控件这样赋值 懒得写委托 要是有十个组件是个蛋疼的事
...
taoerchun 发表于 2015-11-11 10:31
你不用Invoke直接更新richTextBox1的话会报“不是从它创建的线程访问”的错吧
站长苏飞 发表于 2015-11-11 10:40
你猜的吧,我运行这么久也没听说过会有这错
taoerchun 发表于 2015-11-11 12:55
我下载了你的源码,取消掉BeginInvoke后报“线程间操作无效: 从不是创建控件“richTextBox1”的线程访问 ...
站长苏飞 发表于 2013-6-8 08:33
能帮上忙就好,有问题可以来论坛发帖子
馨雨科技 发表于 2015-12-27 10:08
我用ListView 全部都是一样的代码 只是把控件换成了 ListView 依旧还是假死 很奇怪 明明 我也是 在 Threa ...
站长苏飞 发表于 2015-12-27 12:13
发个帖子出来看看具体情况
馨雨科技 发表于 2016-1-16 22:36
不好意思 一直有事情在忙 刚刚上论坛 才看到 现在基本解决了 用的虚拟模式 我上网查了 好像是说是ListVie ...
站长苏飞 发表于 2016-1-18 08:45
恩好,如果再有问题可以直接发帖子。
王二狗 发表于 2016-7-8 14:09
学习了,多线程用委托给datagridview写数据老是卡死,现在用用飞哥的方法试试
MeeSii 发表于 2016-9-8 00:56
为什么我测试用万能框架获取数据就会卡住啊,完全按你的代码写的呀。
站长苏飞 发表于 2016-9-8 10:29
具体问题具体分析,发个帖子贴下相关代码出来看下情况
1229203497 发表于 2018-12-4 08:58
问题已解决 赞一个先
但是后期出现一个问题 线程未关闭
System.InvalidOperationException:“ ...
欢迎光临 苏飞论坛 (http://www.sufeinet.com/) | Powered by Discuz! X3.4 |