0

我有一个清单。我把所有查询输出放在哪里。现在使用线程做一些处理。所以当工作完成后,需要更新列表项的值。请在下面查看我的代码:

公开名单:

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

从数据库中获取数据并操作列表:

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            
   }
4

2 回答 2

1

您需要在数组列表中找到一个item[0]等于 的项目id,并将其设置statusitem[1]。你可以用一个循环来做,像这样

foreach (string[] item in OutboxList) {
    if (item[0] == id) {
        item[1] = status;
        break;
    }
}

或使用 LINQ,如下所示:

var item = OutboxList.FirstOrDefault(a => a[0] == id);
if (item != null) {
    item[1] = status;
}

请注意,您的数据结构并不是特别面向对象的。string如果您将包含七个项目的数组替换为class具有七个字符串字段的 a,您的程序将更具可读性,如下所示:

class OutBoxFields {
    public string Id {get;set;}
    public string Status {get;set;}
    ... // and so on
}
于 2013-09-28T10:22:57.930 回答
1

将数组直接传递给,Thread以便在完成后更新数组。

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 编译器中构建,这很好,否则您需要在循环内使用局部变量。

于 2013-09-28T10:25:36.897 回答