从调用方法获得退货 [英] Get a RETURN from a Invoke method
问题描述
我正在尝试从另一个线程上的列表框项目中读取值.
I am trying to read value from a listbox item that is on another thread.
我试图制作一个新方法来运行invoke命令,我可以设法通过调用方法像添加一样将命令发送到列表框,但是我似乎无法获得响应,我似乎无法获得项目,我尝试了几种方法,一旦将其从空变为字符串,事情就开始变得毛茸茸了...
I tried to make a new method to run the invoke command, I can manage to send a command to the listbox like add via the invoke method but i cant seem to get a response, i cant seem to get the value of the item, i have tried a few ways, once i change it from a void to a string things start to get hairy...
thread t1 = new thread(thethread)
t1.start()
public void thethread()
{
string text = readListBoxSelected(listBox1) + " lala" ;
}
public static string readListBoxSelected(ListBox listbox)
{
if (listbox.InvokeRequired)
{
return (string)listbox.Invoke(
new Func<String>(() => readListBoxSelected(listbox))
);
}
else
{
string varText = listbox.SelectedValue.ToString();
return varText;
}
}
上面是我正在尝试做的一个例子.
Above is a example of what i am trying to do.
这是错误:
System.NullReferenceException原为 用户代码未处理
Message =对象引用未设置为 对象的实例.
来源= * * StackTrace: 在 * * .在e:\ documents中的Form1.readListBoxSelected(ListBox listbox)和 设置\斯科特\我的文档\视觉 工作室 2010 \ Projects ** * * * \ Form1.cs:line 133 在 *** .Form1.<> c_ DisplayClass5.b _3() 在e:\ documents and settings \ scott \ my 文件\视觉工作室 2010 \ Projects ** * * ** \ Form1.cs:line 127 InnerException:
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=** StackTrace: at **.Form1.readListBoxSelected(ListBox listbox) in e:\documents and settings\scott\my documents\visual studio 2010\Projects*****\Form1.cs:line 133 at ***.Form1.<>c_DisplayClass5.b_3() in e:\documents and settings\scott\my documents\visual studio 2010\Projects******\Form1.cs:line 127 InnerException:
我想我的错恰恰是它所说的对象引用未设置为对象实例" .......据我所知,我所有的变量似乎都被声明为公平,我该如何纠正这个吗?
I imagine what is wrong is exactly what it says "Object reference not set to an instance of an object"....... All my variables seem to be declared as fair as i am aware, how can i correct this??
我感觉到我正在错误地处理整个事情.... 0_o 提前致谢, 斯科特
I get the feeling i am going about the entire thing wrongly.... 0_o Thanks in Advance, Scott
推荐答案
尝试一下
public static string readListBoxSelected(ListBox listbox)
{
if (listbox.InvokeRequired)
{
return (string)listbox.Invoke(
new Func<String>(() => readListBoxSelected(listbox))
);
}
else
{
if(istbox.SelectedValue != null)
return listbox.SelectedValue.ToString();
else
return String.Empty
}
}
这篇关于从调用方法获得退货的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!