搞不懂的委托?求一个实例学习,线程类中操作ui线程上面的listview 我不太清楚,你为什么会设计成这样,多线程的处理非常麻烦,建议你还是重新考虑一下设计.如果确实要用,你这块有三个问题需要注意,listview必须由 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我不太清楚,你为什么会设计成这样,多线程的处理非常麻烦,建议你还是重新考虑一下设计.如果确实要用,你这块有三个问题需要注意,1.listview必须由UI线程处理,因此会有IVOKE切换的问题;2.多线程需对同一个listview处理,有一个锁或者队列的问题.3.可能还有一个线程的同步和通信问题.我想如果搞清这三点,至于实现应该是非常简单的事,不需要看什么事例代码. 主线程:第一步:定义委托delegateprivate delegate void setConn(string strConn);private setConn mySetConn = null;第二步:定义一个方法private void DispText(string strConn){ textBox1.Text = strConn;}第三步:初始化委托mySetConn = new setConn(this.DispText);第四步:在工作线程中this.Invoke(mySetConn, "把我显示在textBox1中,谢谢!"); 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 WindowsApplication1{ public partial class Form1 : Form { Thread newThread; delegate void myDelegate(string _strFrame); myDelegate mydelegate = null; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { mydelegate = new myDelegate(ShowText); this.textBox1.Text = "begin"; newThread = new Thread(new ThreadStart(Thread_proc)); newThread.Start(); } private void Thread_proc() { Random a = new Random(); int b = a.Next(100); //this.textBox1.Text = b.ToString(); ShowText(b.ToString()); } private void textBox1_TextChanged(object sender, EventArgs e) { MessageBox.Show("Text changed"); } private void ShowText(string strText) { if (this.textBox1.InvokeRequired) { mydelegate = new myDelegate(ShowText); this.Invoke(mydelegate, new object[] { strText }); } else { if (textBox1.TextLength < 1000) textBox1.Text += strText; else textBox1.Text = strText; } } }} 以上例子,线程里控制ui里的textbox.2005下调试通过 上面几个全部不对,你们写的比我要求的简单多了!ristona(一箭) 说的很对,确实是这几个要求,但是下面几位没有一个达到要求的,你们写的要比我要求的简单多了 http://blog.csdn.net/chendazhi/archive/2006/05/09/715064.aspx 关于c#类的使用请教! c#中timer事件中的问题.给出代码帮忙看下 关于DataGridView bu dong 挑战高手 datagrid问题???????????? 怎样把comboBox绑定到DataGrid中 谢谢 在C#里用到.JS文件的问题 调试问题!!!高分求高手!! 关于,C#中结构和类的引用问题??? 请问如何锁定子窗口 gridview分页问题
1.listview必须由UI线程处理,因此会有IVOKE切换的问题;
2.多线程需对同一个listview处理,有一个锁或者队列的问题.
3.可能还有一个线程的同步和通信问题.我想如果搞清这三点,至于实现应该是非常简单的事,不需要看什么事例代码.
第一步:定义委托delegate
private delegate void setConn(string strConn);
private setConn mySetConn = null;
第二步:定义一个方法
private void DispText(string strConn)
{
textBox1.Text = strConn;
}
第三步:初始化委托
mySetConn = new setConn(this.DispText);
第四步:在工作线程中
this.Invoke(mySetConn, "把我显示在textBox1中,谢谢!");
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace WindowsApplication1
{
public partial class Form1 : Form
{
Thread newThread;
delegate void myDelegate(string _strFrame);
myDelegate mydelegate = null; public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
mydelegate = new myDelegate(ShowText);
this.textBox1.Text = "begin";
newThread = new Thread(new ThreadStart(Thread_proc));
newThread.Start();
}
private void Thread_proc()
{
Random a = new Random();
int b = a.Next(100);
//this.textBox1.Text = b.ToString();
ShowText(b.ToString());
} private void textBox1_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("Text changed");
} private void ShowText(string strText)
{
if (this.textBox1.InvokeRequired)
{
mydelegate = new myDelegate(ShowText);
this.Invoke(mydelegate, new object[] { strText });
}
else
{
if (textBox1.TextLength < 1000)
textBox1.Text += strText;
else
textBox1.Text = strText;
}
}
}
}
ristona(一箭) 说的很对,确实是这几个要求,但是下面几位没有一个达到要求的,你们写的要比我要求的简单多了