问题描述如下:
有一个静态类:
其中含有一个新建线程的静态方法
有一个窗体类:
其中含控件若干
现在窗体类中调用静态类的新建线程方法,问静态类中创建的线程入口函数中如何修改该窗体的控件的属性
环境VS2005最好能给个小DEMO , 在线等待
有一个静态类:
其中含有一个新建线程的静态方法
有一个窗体类:
其中含控件若干
现在窗体类中调用静态类的新建线程方法,问静态类中创建的线程入口函数中如何修改该窗体的控件的属性
环境VS2005最好能给个小DEMO , 在线等待
解决方案 »
- 一个关于根据字符串内容进行排序的问题,请帮忙解决一下,谢谢!
- HTTPS获得验证码文件问题
- c#中如何单元测试?
- 关于数据查询和返回的问题
- c# 能不能水平翻转会动的gif图片
- 对方给我用socket发来的XML格式的数据.我怎么转换成xml文件?最好有代码 谢谢!
- 如何向一个特定IP和端口的主机发送信息,代码怎么实现? 急!急!急!
- winform 如何实现字典的速录
- C# DataGridView里的DataGridViewComboBoxColumn的几个问题
- C#中关于.NetFramework问题
- winform里的控件,比如treeview或者picturebox能不能随着winform窗口的大小而变化
- C#中換行問題--在線等
//代理方法模板
public delegate void Procedure(); //某个事件中开启一个线程
Thread thread = new Thread(new ThreadStart(this.StartInit));
thread.Start();
//线程代理方法, this是指窗体
public virtual void StartInit(){
this.Invoke(
new Procedure(
delegate(){
this.Update();//这就是跨线程操作
this.DialogResult = DialogResult.OK;//比如...
this.Text = "信心很重要";//比如改变标题
}
)
);
this.Invoke(new Procedure(this.Close));//比比调用关闭窗体的方法
}2:希望对你有帮助
public virtual void StartInit(){
this.Invoke(
new Procedure( 有点看不懂啊.................
//////////////////////Procedure代理不是没有参数的吗???
delegate(){
this.Update();//这就是跨线程操作
this.DialogResult = DialogResult.OK;//比如...
this.Text = "信心很重要";//比如改变标题
}
)
);
this.Invoke(new Procedure(this.Close));//比比调用关闭窗体的方法
}
能不能给我写个不在同一个类里的例子
希望对楼主有所启发
delegate void SetTextCallback(string text);
private void SetText(string text)
{
if (this.inforMg.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.inforMg.Text = text;
}
}
public delegate void minvole(string str);
private void button3_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(GetTextFun));
t.Start();
}private void GetTextFun()
{
setText("");
}
private void setText(string str)
{
if (textBox1.InvokeRequired)
{
minvole _minvole = new minvole(setText);
this.Invoke(_myinvole, new object[] { str });
}
else
{
this.textBox1.Text = str;
}
}
参考
new Procedure(), 内在参数是什么一个实际的方法,你下VS写代码会提示的new Procedure(void() target),
就是说它的参数就是一个函数指针类似的东东。
代理和一般类有点特别,建议这方面先了解下!
请把那段代码写到VS测试,没问题,因为是我项目的源码的一段,我们一向是这样用的!
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;using System.Threading;namespace exp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
//某个事件中开启一个线程
Thread thread = new Thread(new ThreadStart(this.StartInit));
thread.Start();
} //代理方法模板
public delegate void Procedure(); //线程代理方法, this是指窗体
public virtual void StartInit()
{
this.Invoke(
new Procedure(
delegate(){
this.Update();//这就是跨线程操作
this.DialogResult = DialogResult.OK;//比如...
this.Text = "信心很重要";//比如改变标题
}
)
);
//this.Invoke(new Procedure(this.Close));//比比调用关闭窗体的方法
}
}
}using System;
using System.Collections.Generic;
using System.Text;using System.Threading;namespace exp
{
class Class1
{
public static Thread myThread; public static void start()
{
myThread=new Thread(new ThreadStart(setText));
myThread.Start();
} private void setText()
{
//////////////如何访问form1中的控件
}
}
}