调用委托后,资源占用哪个线程的? (一个例子)
2008-02-28 14:04
316 查看
创建一个WinForm程序.自带一个Form1.在Form1上放置一个button1
创建一个UserControl控件,在控件上放置一个button1,
把创建好的控件放置到 Form1上,名称为 UserControl1
Form1代码如下:
public partial class Form1 : Form
...{
public Form1()
...{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
...{
lock (userControl1.FLAG)
...{
System.Diagnostics.Debug.WriteLine("button1_Click Locked.");
userControl11.Dispose();
}
System.Diagnostics.Debug.WriteLine("button1_Click Locked.OUT");
}
}
UserControl代码如下:
public partial class UserControl1 : UserControl
...{
public object FLAG = new object();
System.Timers.Timer m_timer = new System.Timers.Timer(500);
public UserControl1()
...{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
...{
m_timer.Elapsed += new System.Timers.ElapsedEventHandler(m_timer_Elapsed);
m_timer.Start();
}
delegate void dlgtGen();
void m_timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
...{
m_timer.Stop();
this.Invoke(new dlgtGen(AddUC)); //注释这段代码,换成 AddUC();试一下:)
}
void AddUC()
...{
lock (FLAG)
...{
System.Diagnostics.Debug.WriteLine("AddUC Locked.");
for (int i = 0; i < 10000; ++i)
...{
for (int j = 0; j < 1000000; ++j)
...{
//System.Threading.Thread.Sleep(1);
//Application.DoEvents();
}
}
}
System.Diagnostics.Debug.WriteLine("AddUC Locked.END");
}
}
运行程序后,先点击 控件上的BUTTON,再点击FORM1上的BUTTON,看看会有什么结果?
实际情况是:点击了控件上的BUTTON后,程序处理假死状态,没有反应.因为控件处于主线程内,timer中调用的委托占用了主线程的资源. 当然界面上的资源也是主线程的资源,所以,只有等待委托调用结束后,界面的资源才会有反应.
这其实就如点击一个BUTTON,在BUTTON中Sleep()一段时间效果是一样的!
然后再把
this.Invoke(new dlgtGen(AddUC)); //注释这段代码,换成 AddUC();试一下:)
这行替换为
AddUC();
运行的结果则完成不同了,因为这时候运行的资源完全是主线程之外的一个线程中
创建一个UserControl控件,在控件上放置一个button1,
把创建好的控件放置到 Form1上,名称为 UserControl1
Form1代码如下:
public partial class Form1 : Form
...{
public Form1()
...{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
...{
lock (userControl1.FLAG)
...{
System.Diagnostics.Debug.WriteLine("button1_Click Locked.");
userControl11.Dispose();
}
System.Diagnostics.Debug.WriteLine("button1_Click Locked.OUT");
}
}
UserControl代码如下:
public partial class UserControl1 : UserControl
...{
public object FLAG = new object();
System.Timers.Timer m_timer = new System.Timers.Timer(500);
public UserControl1()
...{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
...{
m_timer.Elapsed += new System.Timers.ElapsedEventHandler(m_timer_Elapsed);
m_timer.Start();
}
delegate void dlgtGen();
void m_timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
...{
m_timer.Stop();
this.Invoke(new dlgtGen(AddUC)); //注释这段代码,换成 AddUC();试一下:)
}
void AddUC()
...{
lock (FLAG)
...{
System.Diagnostics.Debug.WriteLine("AddUC Locked.");
for (int i = 0; i < 10000; ++i)
...{
for (int j = 0; j < 1000000; ++j)
...{
//System.Threading.Thread.Sleep(1);
//Application.DoEvents();
}
}
}
System.Diagnostics.Debug.WriteLine("AddUC Locked.END");
}
}
运行程序后,先点击 控件上的BUTTON,再点击FORM1上的BUTTON,看看会有什么结果?
实际情况是:点击了控件上的BUTTON后,程序处理假死状态,没有反应.因为控件处于主线程内,timer中调用的委托占用了主线程的资源. 当然界面上的资源也是主线程的资源,所以,只有等待委托调用结束后,界面的资源才会有反应.
这其实就如点击一个BUTTON,在BUTTON中Sleep()一段时间效果是一样的!
然后再把
this.Invoke(new dlgtGen(AddUC)); //注释这段代码,换成 AddUC();试一下:)
这行替换为
AddUC();
运行的结果则完成不同了,因为这时候运行的资源完全是主线程之外的一个线程中
相关文章推荐
- 调用委托后,资源占用哪个线程的?
- [c/c++] 一个进程有多个线程,用什么方法让主线程不退出更好,占用资源最少,效率最高?
- 查看Java哪个线程占用CPU资源
- 章鱼哥出品_VB.NET 线程委托应用实例 一个简单的例子让你轻松掌握委托的用法
- 找出JAVA中占用CUP资源最多的哪个线程
- [c/c++] 一个进程有多个线程,用什么方法让主线程不退出更好,要求占用资源最少,效率最高
- .net C# 跨越线程调用窗体上的控件一个完整的例子
- 在线程里面要改变主进程的一个值,并通知主进程(如进度条),以及资源占用自动延时
- Go调用Java的一个简单例子
- 一个简单的C#多线程间同步的例子
- 一个aspx页面有2个按钮,都是服务器控件,服务器上的DLL如何知道用户点击了哪个按钮,从而按钮的事件函数被调用
- 把对互斥资源的锁机制访问改为增加一个资源管理线程如何?
- 一个在HTML使用JavaScript实现调用Windows Shell.Application的例子。
- 分析占用了大量 CPU 处理时间的是Java 进程中哪个线程
- 分析占用了大量 CPU 处理时间的是Java 进程中哪个线程
- 【微博SDK调用逻辑】微博SDK的调用逻辑,最好自己还是写一个例子,试一下!!!
- 一个分页的类及调用的例子
- 学习Ajax做的一个异步调用例子
- 委托的调用方法与执行线程
- java线程的一个小例子