如何更新列表的项目值? [英] How to update a item value of a list?

查看:64
本文介绍了如何更新列表的项目值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个清单.我把所有查询输出都放在哪里.现在使用 线.因此,当工作完成时,则需要更新列表项的值. 请在下面查看我的代码:

i have a list. Where i put all my query output. Now do some processing using thread. so when work is completed then need to update the list item value. please see my code below:

公开声明的列表:

public static List<string[]> OutboxList = new List<string[]>();

从数据库中获取数据并处理列表:

fetch the data from database and manipulate the list:

OutboxQueryCommand.CommandText = "SELECT top 5 id, status from TableA";      
SqlDataReader OutboxQueryReader = OutboxQueryCommand.ExecuteReader();

while (OutboxQueryReader.Read())
{
    string[] OutBoxFields = new string[7];
    OutBoxFields[0] = OutboxQueryReader["id"].ToString();
    OutBoxFields[1] = OutboxQueryReader["status"].ToString();
    OutboxList.Add(OutBoxFields);
}

foreach (string[] OutBoxFields in OutboxList)
{
    id = OutBoxFields[0];
    status = OutBoxFields[1];

    Thread OutboxThread = new Thread(() => OutboxThreadProcessor(id,status));
    OutboxThread.Start();
}

通过线程调用方法:

 static void OutboxThreadProcessor(string id,string status)
   {
//predefine value of status is "QUE". Process some work here for that perticular item list.if data process success full then need to update 
// the status of the list item 

// need to update the perticular value of that list item here.
How i do it???????
//Say for Example 1-> Success
//            2-> Failed
//            3-> Success            
   }

推荐答案

将数组直接传递到Thread,以便一旦完成就可以更新数组.

Pass the array directly to Thread so that you can update the array once you're done.

static void OutboxThreadProcessor(string[] OutBoxFields)
{
    string id = OutBoxFields[0];
    string status = OutBoxFields[1];

    //Do work

    OutBoxFields[0] = "2";//update the array
    OutBoxFields[1] = "something";
}

这样称呼

Thread OutboxThread = new Thread(() => OutboxThreadProcessor(OutBoxFields));
OutboxThread.Start();

还请注意,在这种情况下,您将关闭循环,如果在c#5.0编译器中进行构建,这很好,否则,您需要在循环内使用局部变量.

Also note that you're closing over the loop in this scenario, this is fine if you're building in c#5.0 compiler this is fine, else you need to use a local variable inside the loop.

这篇关于如何更新列表的项目值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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