利用委托跨线程访问窗体中的控件,怎么TextBox可以访问,ComboBox却不能访问?

emailqjc 2011-09-14 11:22:11
//声明
private delegate string mydelMember();

private string GetSearchWhere()
{
string result = string.Empty;

if (myTextBoxMemberID.Text.Trim() != "")
{
result = " and memberid='" + myTextBoxMemberID.Text.Trim() + "'";
}

if (myTextBoxMemberName.Text.Trim() != "")
{
result += " and membername like '%" + myTextBoxMemberName.Text.Trim() + "%'";
}

if (myTextBoxIDCard.Text.Trim() != "")
{
result += " and idcard = '" + myTextBoxIDCard.Text.Trim() + "'";
}

if (myTextBoxGZDW.Text.Trim() != "")
{
result += " and gzdw like '%" + myTextBoxGZDW.Text.Trim() + "%'";
}

if (myTextBoxjfdy .Text.Trim() != "")
{
result += " and xsjf >= " + myTextBoxjfdy.Text.Trim() + "";
}

if (myTextBoxjfxy .Text.Trim() != "")
{
result += " and xsjf <= " + myTextBoxjfxy.Text.Trim() + "";
}

if (comBoxPricType.IsHandleCreated)
{
if (comBoxPricType.Items.Count > 0)
{
if (comBoxPricType.SelectedIndex >= 0)
{
result += " and prictypeid = '" + comBoxPricType.Items[comBoxPricType.SelectedIndex].ToString().Substring(0, comBoxPricType.Items[comBoxPricType.SelectedIndex].ToString().IndexOf(".")) + "'";
}
}
}
if (myTextBoxZKL.Text.Trim()!="" )
{
result += " and zkrate <" + myTextBoxZKL.Text.Trim() + "";
}
return result;
}

调用:
using (BackgroundWorker backgroundWorker = new BackgroundWorker())
{
// 异步获取数据
backgroundWorker.DoWork += new DoWorkEventHandler(delegate(object o, DoWorkEventArgs workerEventArgs)
{
//this.BeginInvoke(new mydelMember(this.GetSearchWhere));

this.BeginInvoke(new myDelegatDoProcess(doProcess));

mydelMember strSearChWhere = new mydelMember(GetSearchWhere);//调用
string strTemp = strSearChWhere();

fCommandText = "select * from emberinfo " + strTemp;

//string a = this.BeginInvoke(new mydel(GetSearchWhere()));

using (SqlDataAdapter fAdapter = new SqlDataAdapter(fCommandText, fConnection))
{
try
{
fIsCommandIsCancelOrError = false;
fCurCommand = fAdapter.SelectCommand;
fCurCommand.CommandTimeout = 0;// 永不超时
fdsResult = new DataSet();
fAdapter.Fill(fdsResult);
fCurCommand = null;
}
catch (Exception x)
{
fIsCommandIsCancelOrError = true; //查询出错或用户已取消查询
}
}
});

// 数据获取完成,绑定数据
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object o, RunWorkerCompletedEventArgs x)
{
if (!fIsCommandIsCancelOrError)
{
dtResult = fdsResult.Tables[0];
}
if (fIsCommandIsCancelOrError == false)
{
this.tsPressBarQry.Value = 100;
}
this.DialogResult = DialogResult.OK;
});

backgroundWorker.RunWorkerAsync();
}
...全文
253 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
cuiweibin5 2011-09-16
  • 打赏
  • 举报
回复
delegate void cc(string t);
public void print(string t)
{
if (!this.richTextBox1.InvokeRequired)
{
this.richTextBox1.Text += t.ToString();
}
else
{
cc d = new cc(print);
this.richTextBox1.Invoke(d, new object[] { t });
}

}
qq598235031 2011-09-14
  • 打赏
  • 举报
回复
this.BeginInvoke(new myDelegatDoProcess(doProcess));
之后加个判断如果this.iscomplete=true(委托完成)
表示当前的委托完成任务在执行下面的方法
private string GetSearchWhere()
{
if (myTextBoxMemberID.InvokeRequired)
{
myTextBoxMemberID.Invoke(new delegate ...);
}
else
{
...原来的函数代码
}
}
emailqjc 2011-09-14
  • 打赏
  • 举报
回复
我本来就是委托访问
emailqjc 2011-09-14
  • 打赏
  • 举报
回复
to 7楼的:
你哪个方法也不行哈
emailqjc 2011-09-14
  • 打赏
  • 举报
回复
请问是不是在BackgroundWorker 里调用UI界面上的控件得用BeginInvoke

this.BeginInvoke(new mydelMember(this.GetSearchWhere));

可是,利用这个方法调用根本没法在DoWork事件执行前获得GetSearchWhere中返回的条件
只在此山中 2011-09-14
  • 打赏
  • 举报
回复
改成委托访问:
private string GetSearchWhere()
{
if (myTextBoxMemberID.InvokeRequired)
{
myTextBoxMemberID.Invoke(new delegate ...);
}
else
{
...原来的函数代码
}
}
emailqjc 2011-09-14
  • 打赏
  • 举报
回复
同样的myTextBoxGZDW为什么能访问
emailqjc 2011-09-14
  • 打赏
  • 举报
回复
提示:
线程间操作无效: 从不是创建控件“comBoxPricType”的线程访问它。
只在此山中 2011-09-14
  • 打赏
  • 举报
回复
错误提示是什么?
emailqjc 2011-09-14
  • 打赏
  • 举报
回复
to 楼上的:
关键问题在这里:利用委托跨线程访问窗体中的控件,怎么TextBox可以访问,ComboBox却不能访问
liumj2001 2011-09-14
  • 打赏
  • 举报
回复
哈哈。

同样头疼。

检查一下作用域。
Just4life 2011-09-14
  • 打赏
  • 举报
回复
太乱,看着头疼
emailqjc 2011-09-14
  • 打赏
  • 举报
回复
解决了:
把 strTemp = GetSearchWhere()放在线程前执行就可以了

strTemp = GetSearchWhere()
if (!CheckWhere()) { return; }
strTemp = GetSearchWhere();
using (BackgroundWorker backgroundWorker = new BackgroundWorker())
{
// 异步获取数据
backgroundWorker.DoWork += new DoWorkEventHandler(delegate(object o, DoWorkEventArgs workerEventArgs)
{
this.BeginInvoke(new myDelegatDoProcess(doProcess));

fCommandText = "select* from memberinfo where 1=1 " + strTemp;

using (SqlDataAdapter fAdapter = new SqlDataAdapter(fCommandText, fConnection))
{
try
{
fIsCommandIsCancelOrError = false;
fCurCommand = fAdapter.SelectCommand;
fCurCommand.CommandTimeout = 0;// 永不超时
fdsResult = new DataSet();
fAdapter.Fill(fdsResult);
fCurCommand = null;
}
catch (Exception x)
{
fIsCommandIsCancelOrError = true; //查询出错或用户已取消查询
}
}
});

// 数据获取完成,绑定数据
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object o, RunWorkerCompletedEventArgs x)
{
if (!fIsCommandIsCancelOrError)
{
dtResult = fdsResult.Tables[0];
}
if (fIsCommandIsCancelOrError == false)
{
this.tsPressBarQry.Value = 100;
}
this.DialogResult = DialogResult.OK;
});

//backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(delegate(object o, ProgressChangedEventArgs x)
//{
// this.BeginInvoke(new myDelegatDoProcess(doProcess));
//});

backgroundWorker.RunWorkerAsync();
}

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧