c# winform跨线程操作控件
我想在子线程里控制主线程checkbox控件的checked属性,请问怎么写代码,若是用委托又要怎么写,请详细说说~谢谢...
我想在子线程里控制主线程checkbox控件的checked属性,请问怎么写代码,若是用委托又要怎么写,请详细说说~谢谢
展开
3个回答
展开全部
在子线程里不能直接使用主线程创建的控件...需要建立委托,在委托事件中写操作主线程控件的代码,在子线程中调用委托操作主线程控件。
下面是写的一个例子:
public partial class Form1 : Form
{
Thread thread=null;
int n = 0;
bool severIsRun=false;//用来村示线程是否启动
private delegate void myDelegate(string str);//定义委托
private void setRich(string str)//委托
{
if (this.textBox1.InvokeRequired)
{
myDelegate md = new myDelegate(this.setRich);
this.Invoke(md, new object[] { str });
}
else
this.textBox1.Text =str;
}
public Form1()
{
InitializeComponent();
}
private void run()
{
while(true)
{
setRich((n++).toString());
Thread.Sleep(1000);
}
}
private void button1_Click(object sender, EventArgs e)
{
if(!severIsRun)
{
ThreadStart ts =new ThreadStart(run);
Thread = new Thread(ts);
severIsRun=true;
thread.Start();
}
}
private void button2_Click(object sender, EventArgs e)
{
if(severIsRun)
{
thread.Abort();
severIsRun=false;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(severIsRun)
{
thread.Abort();
severIsRun=false;
}
}
}
下面是写的一个例子:
public partial class Form1 : Form
{
Thread thread=null;
int n = 0;
bool severIsRun=false;//用来村示线程是否启动
private delegate void myDelegate(string str);//定义委托
private void setRich(string str)//委托
{
if (this.textBox1.InvokeRequired)
{
myDelegate md = new myDelegate(this.setRich);
this.Invoke(md, new object[] { str });
}
else
this.textBox1.Text =str;
}
public Form1()
{
InitializeComponent();
}
private void run()
{
while(true)
{
setRich((n++).toString());
Thread.Sleep(1000);
}
}
private void button1_Click(object sender, EventArgs e)
{
if(!severIsRun)
{
ThreadStart ts =new ThreadStart(run);
Thread = new Thread(ts);
severIsRun=true;
thread.Start();
}
}
private void button2_Click(object sender, EventArgs e)
{
if(severIsRun)
{
thread.Abort();
severIsRun=false;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(severIsRun)
{
thread.Abort();
severIsRun=false;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public delegate void MyInvoke(bool checked);
private void button_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(fun));
t.Start();
}
private void fun()
{
SetChecked(false);
}
private void SetChecked(bool checked)
{
if (checkbox.InvokeRequired)
{
MyInvoke _myInvoke = new MyInvoke(SetChecked);
this.Invoke(_myInvoke, new object[] { checked });
}
else
{
this.checkbox.Checked = checked;
}
}
private void button_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(fun));
t.Start();
}
private void fun()
{
SetChecked(false);
}
private void SetChecked(bool checked)
{
if (checkbox.InvokeRequired)
{
MyInvoke _myInvoke = new MyInvoke(SetChecked);
this.Invoke(_myInvoke, new object[] { checked });
}
else
{
this.checkbox.Checked = checked;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
委托的
private void delegate ChangeChkBoxStateDelegate();
private void ChangeChkBoxState() //方法若有参数,上面委托的定义中也必须有相同的参数
{
if(this.InvokeRequired)
{
ChangeChkBoxStateDelegate newDelegate = new ChangeChkBoxStateDelegate();
this.Invoke(newDelegate,new object[]{});
}
else
{
checkbox1.Checked = !checkbox1.Checked;
}
}
private void delegate ChangeChkBoxStateDelegate();
private void ChangeChkBoxState() //方法若有参数,上面委托的定义中也必须有相同的参数
{
if(this.InvokeRequired)
{
ChangeChkBoxStateDelegate newDelegate = new ChangeChkBoxStateDelegate();
this.Invoke(newDelegate,new object[]{});
}
else
{
checkbox1.Checked = !checkbox1.Checked;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询