C#中异步回调的问题(回调函数返回值调用)。??如何将tag_mreade的str传递给 carlist??(代码如下)
privatevoidbtn_rtag_Click(objectsender,EventArgse){//利用异步回调获取标签stringcarlist;boolb_ta...
private void btn_rtag_Click(object sender, EventArgs e)
{//利用异步回调获取标签
string carlist;
bool b_tag=true;
Tag_reader tr;
tr = new Tag_reader(tag_mreade);
IAsyncResult tg = tr.BeginInvoke(b_tag, TagReadCompleted,tr);
do
{
textBox1.Text = textBox1.Text + ".";
}
while (!tg.IsCompleted);
//??如何将tag_mreade的str传递给 carlist??
textBox1.AppendText( carlist);
}
private delegate string Tag_reader(bool bl_cmdd);
private string tag_mreade(bool bl_cmd)
{
string str;
try
{
if (bl_cmd)
{
str = card.TagList;//
}
else { str = "查看tag_reade的布尔值,未进行读取"; }
return str;
}
catch (Exception e1) { return e1.Message.ToString(); }
}
private void TagReadCompleted(IAsyncResult asyncResult)
{//回调函数
string str_tags;
if (asyncResult == null) {str_tags= ""; }
str_tags = (asyncResult.AsyncState as Tag_reader).EndInvoke(asyncResult);
} 展开
{//利用异步回调获取标签
string carlist;
bool b_tag=true;
Tag_reader tr;
tr = new Tag_reader(tag_mreade);
IAsyncResult tg = tr.BeginInvoke(b_tag, TagReadCompleted,tr);
do
{
textBox1.Text = textBox1.Text + ".";
}
while (!tg.IsCompleted);
//??如何将tag_mreade的str传递给 carlist??
textBox1.AppendText( carlist);
}
private delegate string Tag_reader(bool bl_cmdd);
private string tag_mreade(bool bl_cmd)
{
string str;
try
{
if (bl_cmd)
{
str = card.TagList;//
}
else { str = "查看tag_reade的布尔值,未进行读取"; }
return str;
}
catch (Exception e1) { return e1.Message.ToString(); }
}
private void TagReadCompleted(IAsyncResult asyncResult)
{//回调函数
string str_tags;
if (asyncResult == null) {str_tags= ""; }
str_tags = (asyncResult.AsyncState as Tag_reader).EndInvoke(asyncResult);
} 展开
1个回答
展开全部
你这写法根本都不对,EndInvoke怎么写到回调函数里去了
判断执行完不需要while循环判断,直接执行EndInvoke最自动阻塞,EndInvoke的参数里就有返回值可以解决你这问题
仔细看看:
http://msdn.microsoft.com/zh-cn/library/2e08f6yc.aspx
判断执行完不需要while循环判断,直接执行EndInvoke最自动阻塞,EndInvoke的参数里就有返回值可以解决你这问题
仔细看看:
http://msdn.microsoft.com/zh-cn/library/2e08f6yc.aspx
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询