如何启用和禁用Thread内的按钮? [英] How to enable and disable a button inside of a Thread ?

查看:54
本文介绍了如何启用和禁用Thread内的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何启用和禁用线程内的按钮



i我在线程内部创建了一个长进程,同时我想禁用进程启动按钮





使用此代码发现一些错误

_________________________________________________________________

private void过程()

{

btnCreate.Enabled = false; chkGroup.Enabled = false;

//漫长的过程

}







______________________On btn点击__________________________

线程创建=新线程(进程);

create.IsBackground = true;

create.Start();

How to enable and disable a button inside of a Thread

i am created a long process inside of a thread, at the same time i want to disable process start button


using this code some errors found
_________________________________________________________________
private void process()
{
btnCreate.Enabled = false; chkGroup.Enabled = false;
//long process
}



______________________On btn Click_________________________________
Thread create = new Thread(process);
create.IsBackground = true;
create.Start();

推荐答案

private void process()
{
btnCreate.Invoke((MethodInvoker) delegate { btnCreate.Enabled = false; chkGroup.Enabled = false; });
//long process
}
 
Thread create = new Thread(new ThreadStart(process));
create.IsBackground = true;
create.Start();





您必须将委托传递给Thread对象,该对象的类型为ThreadStart。然后,要更新UI,您必须使用Control.Invoke方法在UI线程上更新它。



You have to pass a delegate to the Thread object, which is of type ThreadStart. Then for updating the UI you have to update it on the UI thread, using the Control.Invoke method.


这篇关于如何启用和禁用Thread内的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆