匿名代理解决winform跨线程访问控件
2009-01-14 09:40
246 查看
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 AccessControl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread newthread = new Thread(new ThreadStart(BackgroundProcess));
newthread.Start();
}
/// <summary>
/// 定义一个代理
/// </summary>
private delegate void CrossThreadOperationControl();
private void BackgroundProcess()
{
// 将代理实例化为一个匿名代理
CrossThreadOperationControl CrossDelete = delegate()
{
int i = 1;
while (i<5)
{
// 向列表框增加一个项目
listBox1.Items.Add("Item " + i.ToString());
i++;
}
label1.Text = "我在新线程里访问这个lable!";
listBox1.Items.Add(label1.Text);
} ;
listBox1.Invoke(CrossDelete);
}
}
}
原文参照:/article/5914172.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 AccessControl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread newthread = new Thread(new ThreadStart(BackgroundProcess));
newthread.Start();
}
/// <summary>
/// 定义一个代理
/// </summary>
private delegate void CrossThreadOperationControl();
private void BackgroundProcess()
{
// 将代理实例化为一个匿名代理
CrossThreadOperationControl CrossDelete = delegate()
{
int i = 1;
while (i<5)
{
// 向列表框增加一个项目
listBox1.Items.Add("Item " + i.ToString());
i++;
}
label1.Text = "我在新线程里访问这个lable!";
listBox1.Items.Add(label1.Text);
} ;
listBox1.Invoke(CrossDelete);
}
}
}
原文参照:/article/5914172.html
相关文章推荐
- WinForm(C#)中跨线程访问控件的解决方法
- c# winform InvokeRequired 解决跨线程访问控件
- c#中使用多线程访问winform中控件的若干问题 解决线程间操作无效: 从不是创建控件的线程访问它
- WinForm(C#)中跨线程访问控件的解决方法
- WinForm解决多线程操作控件问题 线程间操作无效: 从不是创建控件的线程访问它"
- C#之Winform跨线程访问控件
- Winform 线程间访问控件
- BackGroundWorker解决 线程间操作无效: 从不是创建控件的线程访问它
- C# WinForm跨线程访问控件
- 线程间操作无效 从不是创建控件 的线程访问它。——解决方法
- 总是提示线程无法访问控件,加入下面的代码就解决了
- 线程间操作无效: 从不是创建控件“”的线程访问它~~~的解决方法~
- C#中跨线程访问控件问题解决方案分享
- Winform中多线程的控件访问引发的“线程间操作无效,从不是创建控件的线程访问它”
- BackGroundWorker解决“线程间操作无效: 从不是创建控件的线程访问它”
- C#中跨线程访问控件问题解决方案
- 教你如何解决“线程间操作无效: 从不是创建控件的线程访问它”
- 解决"从不是创建控件的线程访问它"
- c#使用MethodInvoker解决跨线程访问控件
- C# 跨线程访问控件的解决方法