我正在尝试这样做:
string Proxy = listBox3.SelectedIndex.ToString();
非常简单吧?
但是在交叉线程中我遇到了这个错误:
跨线程操作无效:控制'listBox3'从其创建的线程以外的线程访问。
所以我用Google搜索并发现了这个:
this.Invoke((MethodInvoker)(() => listBox3.Items.Add(Item2add)));
用这种想法尝试它会起作用:
string Proxy = listBox3.Invoke((MethodInvoker)(() => listBox3.SelectedIndex.ToString()));
但是我收到了这个错误:
附加信息:未将对象引用设置为对象的实例。
答案 0 :(得分:0)
您可能需要考虑使用一种方法来围绕跨线程更新和winforms中的读取包装(令人惊讶的棘手)逻辑。我在这里创建了一个:Is it appropriate to extend Control to provide consistently safe Invoke/BeginInvoke functionality?
看起来像这样:
/// <summary>
/// Execute a method on the control's owning thread.
/// </summary>
/// <param name="uiElement">The control that is being updated.</param>
/// <param name="updater">The method that updates uiElement.</param>
/// <param name="forceSynchronous">True to force synchronous execution of
/// updater. False to allow asynchronous execution if the call is marshalled
/// from a non-GUI thread. If the method is called on the GUI thread,
/// execution is always synchronous.</param>
public static void SafeInvoke(this Control uiElement, Action updater, bool forceSynchronous)
{
if (uiElement == null)
{
throw new ArgumentNullException("uiElement");
}
if (uiElement.InvokeRequired)
{
if (forceSynchronous)
{
uiElement.Invoke((Action)delegate { SafeInvoke(uiElement, updater, forceSynchronous); });
}
else
{
uiElement.BeginInvoke((Action)delegate { SafeInvoke(uiElement, updater, forceSynchronous); });
}
}
else
{
if (!uiElement.IsHandleCreated)
{
// Do nothing if the handle isn't created already. The user's responsible
// for ensuring that the handle they give us exists.
return;
}
if (uiElement.IsDisposed)
{
throw new ObjectDisposedException("Control is already disposed.");
}
updater();
}
}
答案 1 :(得分:0)
First off, that code there shouldn't even let you compile. It wouldn't let you implicitly cast from an Object to a String.
THE ONLY WAY I could see this producing this error is if listBox3 doesn't exist on the thread you're calling this from. Make sure you're calling it from the UI thread (assuming this is where your listBox is).
Try this code and see if it helps:
string Proxy;
listBox3.Invoke((MethodInvoker)(() => Proxy = listBox3.SelectedIndex.ToString()));