c#中如何跨线程调用windows控件
5个回答
展开全部
在辅助线程调用(在创建辅助线程时可将此方法通过delegate传到辅助线程中)下面的方法InvokeControl();
//写在主线程中(windows控件)
private void InvokeControl()
{
if (this.InvokeRequired)
this.Invoke(new DelegateChangeText(ChangeText));
else
this.ChangeText();
}
private void ChangeText()
{
this.TextBox.Text = "sd";
}
public delegate void DelegateChangeText();
//写在主线程中(windows控件)
private void InvokeControl()
{
if (this.InvokeRequired)
this.Invoke(new DelegateChangeText(ChangeText));
else
this.ChangeText();
}
private void ChangeText()
{
this.TextBox.Text = "sd";
}
public delegate void DelegateChangeText();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
定义委托,然后用invoke
private
void
ucMidPartsid_Load(object
sender,
EventArgs
e)
{
if
(is_Load
==
true)
{
thread
=
new
Thread(new
ThreadStart(this.LoadData));
thread.Start();
}
//if
(datafinish
!=
null)
//{
//
datafinish(this,
e);
//}
}
private
void
LoadData()
{
QueryDataCallBack
deleg;
deleg
=
new
QueryDataCallBack(this.DataBinding);
this.Invoke(deleg,null);
}
private
void
DataBinding()
{
partstableAdapter
=
new
GreatWall.SCM.Parts.DataAccess.DAParts.PartsTableAdapter();
partstableAdapter.Fill(dsParts.GB_PARTS);
cobMidPartsid.DataSource
=
dsParts.GB_PARTS;
cobMidPartsid.DisplayMember
=
"B_PARTSID";
cobMidPartsid.ValueMember
=
"B_ID";
EventArgs
e
=
new
EventArgs();
if
(datafinish
!=
null)
{
finish
=
true;
datafinish(this,
e);
}
}
private
void
ucMidPartsid_Load(object
sender,
EventArgs
e)
{
if
(is_Load
==
true)
{
thread
=
new
Thread(new
ThreadStart(this.LoadData));
thread.Start();
}
//if
(datafinish
!=
null)
//{
//
datafinish(this,
e);
//}
}
private
void
LoadData()
{
QueryDataCallBack
deleg;
deleg
=
new
QueryDataCallBack(this.DataBinding);
this.Invoke(deleg,null);
}
private
void
DataBinding()
{
partstableAdapter
=
new
GreatWall.SCM.Parts.DataAccess.DAParts.PartsTableAdapter();
partstableAdapter.Fill(dsParts.GB_PARTS);
cobMidPartsid.DataSource
=
dsParts.GB_PARTS;
cobMidPartsid.DisplayMember
=
"B_PARTSID";
cobMidPartsid.ValueMember
=
"B_ID";
EventArgs
e
=
new
EventArgs();
if
(datafinish
!=
null)
{
finish
=
true;
datafinish(this,
e);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在构造函数里使用属性
CheckForIllegalCrossThreadCalls 值设置为 false
即 public Form1()
{
CheckForIllegalCrossThreadCalls = false;
}
CheckForIllegalCrossThreadCalls 值设置为 false
即 public Form1()
{
CheckForIllegalCrossThreadCalls = false;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
定义委托,然后用invoke
private void ucMidPartsid_Load(object sender, EventArgs e)
{
if (is_Load == true)
{
thread = new Thread(new ThreadStart(this.LoadData));
thread.Start();
}
//if (datafinish != null)
//{
// datafinish(this, e);
//}
}
private void LoadData()
{
QueryDataCallBack deleg;
deleg = new QueryDataCallBack(this.DataBinding);
this.Invoke(deleg,null);
}
private void DataBinding()
{
partstableAdapter = new GreatWall.SCM.Parts.DataAccess.DAParts.PartsTableAdapter();
partstableAdapter.Fill(dsParts.GB_PARTS);
cobMidPartsid.DataSource = dsParts.GB_PARTS;
cobMidPartsid.DisplayMember = "B_PARTSID";
cobMidPartsid.ValueMember = "B_ID";
EventArgs e = new EventArgs();
if (datafinish != null)
{
finish = true;
datafinish(this, e);
}
}
private void ucMidPartsid_Load(object sender, EventArgs e)
{
if (is_Load == true)
{
thread = new Thread(new ThreadStart(this.LoadData));
thread.Start();
}
//if (datafinish != null)
//{
// datafinish(this, e);
//}
}
private void LoadData()
{
QueryDataCallBack deleg;
deleg = new QueryDataCallBack(this.DataBinding);
this.Invoke(deleg,null);
}
private void DataBinding()
{
partstableAdapter = new GreatWall.SCM.Parts.DataAccess.DAParts.PartsTableAdapter();
partstableAdapter.Fill(dsParts.GB_PARTS);
cobMidPartsid.DataSource = dsParts.GB_PARTS;
cobMidPartsid.DisplayMember = "B_PARTSID";
cobMidPartsid.ValueMember = "B_ID";
EventArgs e = new EventArgs();
if (datafinish != null)
{
finish = true;
datafinish(this, e);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
定义委托 delegate
调用 Invoke
提问之前先利用baidu搜索下吧
调用 Invoke
提问之前先利用baidu搜索下吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询