太难理解了,泪奔求解中... 本帖最后由 b2bsoho 于 2011-03-25 22:30:34 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 UI 应该就是界面委托 - 事件http://www.cnblogs.com/jimmyzhang/archive/2007/09/23/903360.html线程 -- 这个要看点操作系统知识 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 Te{ //未做异常处理,仅做简要说明 public partial class Form1 : Form { //创建一个多线程的示例 Thread _BgThread; //创建一个timer1,在timer1的tick事件中让Number的值每次递增加1 System.Windows.Forms.Timer timer1; //用于测试多线程状态的数字 int Number = 0; public Form1() { InitializeComponent(); timer1 = new System.Windows.Forms.Timer(); timer1.Tick += new EventHandler(timer1_Tick); timer1.Interval = 200; } void timer1_Tick(object sender, EventArgs e) { Number++; } //运行在多线程中的方法 void ThreadMethod() { while (true) { //如果不用多线程的话,下面这句话是正确的 //button1.Text = "运行在多线程中的程序" + Number; //但如果运行多线程的话,必须调用委托 this.Invoke(new delegateMethod(ShowNumber)); //如果需要传参数的话可以使用下面方法 //this.Invoke(new delegate(method),new object[]{method中的参数成员}); } } //委托 delegate void delegateMethod(); void ShowNumber() { button1.Text = "运行在多线程中的程序" + Number; } private void Form1_Load(object sender, EventArgs e) { _BgThread = new Thread(new ThreadStart(ThreadMethod)); _BgThread.Start(); timer1.Start(); } }} 以上代码是在VS2005中写的,拖一个窗体,一个BUTTON控件就行了。至于事件,它就是程序通知你某件事情己经完成或即要完成的行为,事件是不是带数据,要看它是不是需要。一个简单的例子Form的load事件是不需要传值的,它仅仅是告诉你Form己经加载完成了,而它的另外一个事件paint则需要参数,某些时候我们需要Form的Graphics及ClipRectangle才能完成我们想要的功能(重绘) 1.多线程 - 委托 - 事件 - UI 线程=进程中某个单一顺序的控制流(进程就是你执行的程序)委托=简单的概念,类似为c中的函数指针,可以把其他方法做为参数进行传递的方法事件=封装了windows消息的类UI=User Interface 用户界面2.事件委托的 数据传递 和 更新UI的数据传递 的实现问题这个没看懂你在问什么另:1.普通事件和带数据事件有什么区别?带数据的事件会是哪类的事件?区别就是一个有传数据一个没传?囧按钮的click就是个带数据的事件2.一个线程(不是主线程)要更新UI控件是不是这个线程上也要 事件委托 来通知UI上的控件更新?public delegate void MyInvoke(string str);private void button9_Click(object sender, EventArgs e) { //_myInvoke = new MyInvoke(SetText); //CheckForIllegalCrossThreadCalls = false; Thread t = new Thread(new ThreadStart(fun)); t.Start(); } private void fun() { //_myInvoke("dddd"); SetText("ddd"); } private void SetText(string s) { if (textBox6.InvokeRequired) { MyInvoke _myInvoke = new MyInvoke(SetText); this.Invoke(_myInvoke, new object[] { s }); } else { this.textBox6.Text = s; } }本人尝试做个爬虫的小程序,在以上几个问题上耗了几天没个头绪 事件也好,委托也好,最终去执行的是一个方法,方法可以有参数,当然就可以传数据了。另外,不同线程控制的资源,好像是必须要通过委托来使用的。比如在winform里,主线程创建的进度条,其它线程要更改,要通过委托来调用更改进度条的方法比如 this.BeginInvoke(new MyDelegate(SetProg), z); winform怎么隐藏子页面左上角的图标 求助关于W7图标的问题 怎么做类似ms visio一样的画流程图的东西?, vs2010的问题 请问一个关于C#打包的问题 如何将Winform中的某一UserControl全屏显示? 有两个DataTable,如何将下面两个DataTable合成一个DataTable,急! 自己写的一个C#版定时关机器 帮忙一下谢谢 求教c#怎么用socket发送自定义的List给局域网的电脑 跪求:C# 程序出现提示音代码 C#中picturebox的paint 事件
http://www.cnblogs.com/jimmyzhang/archive/2007/09/23/903360.html线程 -- 这个要看点操作系统知识
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Te
{
//未做异常处理,仅做简要说明
public partial class Form1 : Form
{
//创建一个多线程的示例
Thread _BgThread;
//创建一个timer1,在timer1的tick事件中让Number的值每次递增加1
System.Windows.Forms.Timer timer1;
//用于测试多线程状态的数字
int Number = 0;
public Form1()
{
InitializeComponent();
timer1 = new System.Windows.Forms.Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 200;
} void timer1_Tick(object sender, EventArgs e)
{
Number++;
}
//运行在多线程中的方法
void ThreadMethod()
{
while (true)
{
//如果不用多线程的话,下面这句话是正确的
//button1.Text = "运行在多线程中的程序" + Number;
//但如果运行多线程的话,必须调用委托
this.Invoke(new delegateMethod(ShowNumber));
//如果需要传参数的话可以使用下面方法
//this.Invoke(new delegate(method),new object[]{method中的参数成员});
}
}
//委托
delegate void delegateMethod();
void ShowNumber()
{
button1.Text = "运行在多线程中的程序" + Number;
}
private void Form1_Load(object sender, EventArgs e)
{
_BgThread = new Thread(new ThreadStart(ThreadMethod));
_BgThread.Start();
timer1.Start(); }
}
}
至于事件,它就是程序通知你某件事情己经完成或即要完成的行为,事件是不是带数据,要看它是不是需要。
一个简单的例子Form的load事件是不需要传值的,它仅仅是告诉你Form己经加载完成了,而它的另外一个事件paint则需要参数,某些时候我们需要Form的Graphics及ClipRectangle才能完成我们想要的功能(重绘)
线程=进程中某个单一顺序的控制流(进程就是你执行的程序)
委托=简单的概念,类似为c中的函数指针,可以把其他方法做为参数进行传递的方法
事件=封装了windows消息的类
UI=User Interface 用户界面
2.事件委托的 数据传递 和 更新UI的数据传递 的实现问题
这个没看懂你在问什么另:
1.普通事件和带数据事件有什么区别?带数据的事件会是哪类的事件?
区别就是一个有传数据一个没传?囧
按钮的click就是个带数据的事件2.一个线程(不是主线程)要更新UI控件是不是这个线程上也要 事件委托 来通知UI上的控件更新?
public delegate void MyInvoke(string str);
private void button9_Click(object sender, EventArgs e)
{
//_myInvoke = new MyInvoke(SetText);
//CheckForIllegalCrossThreadCalls = false;
Thread t = new Thread(new ThreadStart(fun));
t.Start();
} private void fun()
{
//_myInvoke("dddd");
SetText("ddd");
}
private void SetText(string s)
{
if (textBox6.InvokeRequired)
{
MyInvoke _myInvoke = new MyInvoke(SetText);
this.Invoke(_myInvoke, new object[] { s });
}
else
{
this.textBox6.Text = s;
}
}
本人尝试做个爬虫的小程序,在以上几个问题上耗了几天没个头绪
比如在winform里,主线程创建的进度条,其它线程要更改,要通过委托来调用更改进度条的方法比如 this.BeginInvoke(new MyDelegate(SetProg), z);