传递参数的BackgroundWorker [英] Passing Parameter to Backgroundworker

查看:225
本文介绍了传递参数的BackgroundWorker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进出口运行背景工人一个C#代码。林通过具有foreach循环和传递的foreach变量作为参数BackgroundWorker的循环IM里面impelmenting它。但问题是,每当我运行的代码只有一个随机值,最有可能在GridView的最后一行作为参数传递。该代码是这里

 的foreach(在dataGridView3.Rows的DataGridViewRow行)
{
BackgroundWorker的工人=新的BackgroundWorker();
worker.WorkerSupportsCancellation = TRUE;
worker.DoWork + =代表
{
数据= dataGridView3.Rows [row.Index] .Cells [0] .Value.ToString();
的rowIndex = row.Index;
DATA1 = ROS [0] .Cells [0] .Value.ToString();
};

worker.RunWorkerAync();
}


解决方案

通过发送参数作为尝试

  worker.DoWork + =委托(对象S,DoWorkEventArgs参数) 
{
的DataGridViewRow DGR =(的DataGridViewRow)args.Argument;
数据= dataGridView3.Rows [dgr.Index] .Cells [0] .Value.ToString();
的rowIndex = dgr.Index;
DATA1 = DGR [0] .Cells [0] .Value.ToString();
};

worker.RunWorkerAsync(行);


Im running a c# code with background worker. Im impelmenting it by having a foreach loop and inside the loop im passing the foreach variable as parameter for the Backgroundworker. But the problem is that whenever i run the code only single random value, most probably the last row in the gridview is passed as parameter. The code is as here

foreach (DataGridViewRow row in dataGridView3.Rows)
{
    BackgroundWorker worker = new BackgroundWorker();
    worker.WorkerSupportsCancellation = true;
    worker.DoWork += delegate
    {
        data = dataGridView3.Rows[row.Index].Cells[0].Value.ToString();
        rowindex = row.Index;
        data1 = ros[0].Cells[0].Value.ToString();
    };

    worker.RunWorkerAync();
}

解决方案

try by sending parameter as row

worker.DoWork += delegate(object s, DoWorkEventArgs args)
{
    DataGridViewRow  dgr = (DataGridViewRow)args.Argument;
    data = dataGridView3.Rows[dgr.Index].Cells[0].Value.ToString();
    rowindex = dgr.Index;
    data1 = dgr[0].Cells[0].Value.ToString();
};

worker.RunWorkerAsync(row);

这篇关于传递参数的BackgroundWorker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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