在后台工作调用 [英] invoke during background worker

查看:130
本文介绍了在后台工作调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用这样:字符串input_ip_r = listView1.Items [LC] .SubItems [1]。文本;
所以我用

I need to invoke this: string input_ip_r = listView1.Items[lc].SubItems[1].Text; so I used

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

这工作,但现在我已经把它变成一个的BackgroundWorker 和使用本

This worked but now I have put it into a BackgroundWorker and using this

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

它给你只能运行一个错误的BackgroundWorker 一次。

那么,如何调用,而在的BackgroundWorker

So how do I invoke while in the Backgroundworker?

推荐答案

我不太清楚你想怎么使用的值,而只是给你一个例子,你可以很容易地只是这样做的的BackgroundWorker 主题:

I'm not quite sure how you want to use the values, but just to give you an example, you could easily just do this in the BackgroundWorker thread:

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;
    }));
}

请参阅这个答案做同样的其他方式(这是> =净3.5)

See this answer for other ways of doing the same (this is for >= .Net 3.5)

这篇关于在后台工作调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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