我需要调用它:string input_ip_r = listView1.Items[lc].SubItems[1].Text;
所以我用

if (InvokeRequired)
{
    this.Invoke(new MethodInvoker(function));
    return;
}


这可行,但是现在我将其放入BackgroundWorker并使用它

if (InvokeRequired)
{
    this.Invoke(new MethodInvoker(bw.RunWorkerAsync));
    return;
}


它给出一个错误,您一次只能运行BackgroundWorker

那么如何在Backgroundworker中调用?

最佳答案

我不太确定您想如何使用这些值,但是仅举一个例子,您可以轻松地在BackgroundWorker线程中执行此操作:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    string input_ip_r = "";
    this.Invoke(new Action(() =>
    {
        // Don't know what "lc" is (a loop variable?)
        input_ip_r = listView1.Items[lc].SubItems[1].Text;
    }));
}


有关其他相同方法,请参见this answer(这用于> = .Net 3.5)

10-04 23:05
查看更多