使用反跨线程函数返回值 [英] Using an anti-cross-thread function to return a value

查看:28
本文介绍了使用反跨线程函数返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码来绕过每个人存在的祸根

I have this code to get around the bane of everyone's existence

delegate int GetSelectedIndicesCountCallback(ListBox thing);
private int GetSelectedIndicesCount(ListBox thing)
{
 if (this.InvokeRequired)
 {
  GetSelectedIndicesCountCallback d = new GetSelectedIndicesCountCallback(GetSelectedIndicesCount);
  Invoke(d, new object[] { thing });
 }
 else
 {
  return thing.SelectedIndices.Count;
 }
 return 0;
}

return 0 在那里是因为没有它它会出错.但是,它总是返回0.我不知道如何让它返回另一个值.

The return 0 being there because it'd error without it. However, it always returns 0. I don't know how to get it to return the other value.

推荐答案

调用 Control.Invoke 将返回您的方法的值.您所要做的就是将其转换为 int 并返回.

The call to Control.Invoke will return the value of your method. All you have to do is cast it to int and return it.

return (int)Invoke(d, new object[] { thing });

这篇关于使用反跨线程函数返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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