简体   繁体   English

C# - 循环通过从新线程传递的对象参数

[英]C# - Looping through object parameter that was passed from new thread

I am trying to start a new thread and pass an argument to one of my methods. 我正在尝试启动一个新线程并将参数传递给我的一个方法。 The argument is a List<string> and contains about 20 string items. 参数是List<string> ,包含大约20个字符串项。 I can pass the array fine using the following code: 我可以使用以下代码传递数组:

List<string> strList = new List<string>();

Thread newThread = new Thread(new ParameterizedThreadStart(Class.Method));
newThread.Start(strList);

My Method is defined as: 我的方法定义为:

public void Method(object strList)
{
//code
}

My question then is how can I run a foreach loop through each string that is contained in this object? 那么我的问题是如何通过此对象中包含的每个字符串运行foreach循环?

Three options: 三种选择:

  • Use ParameterizedThreadStart as you are, and cast within the method: 按原样使用ParameterizedThreadStart ,并在方法中强制转换:

     public void Method(object strList) { List<string> list = (List<string>) strList; // Use list here } 
  • Use an anonymous function to capture the variable in a strongly typed way, and call a strongly-typed method from the anonymous function: 使用匿名函数以强类型方式捕获变量,并从匿名函数调用强类型方法:

     List<string> strList = new List<string>(); ThreadStart action = () => Method(strList); new Thread(action).Start(); ... public void Method(List<string> list) { // Use list here } 
  • Use a higher-level abstraction such as the Task Parallel Library or Parallel LINQ instead; 使用更高级别的抽象,例如任务并行库或并行LINQ; depending on what you're doing, this may make things simpler. 取决于你正在做什么,这可能会使事情变得更简单。

If you do want to start a new thread, I'd use the second approach - keep the "dirtiness" localized to the method starting a new thread. 如果想开始一个新的线程,我会使用第二种方法-放置“肮脏”本地化开始一个新的线程的方法。 Any of these approaches will work though. 任何这些方法都可行。 Note that if you had more than one piece of information to pass to the new thread, the second approach ends up being simpler than creating a Tuple and unpacking it. 请注意,如果您有多条信息传递给新线程,则第二种方法最终比创建Tuple并解Tuple更简单。

You have to typecast the object to your list type like eg the following: 您必须将object类型转换为列表类型,例如:

public void Method(object strList)
{
    var list = (List<string>)strList;
    foreach ( var s in list )
    {
        // Do something.
    }
}

您必须将对象强制转换为实际类型,然后您才能使用foreach。

Cast your object with (List<string> ) and then use the iteration through it 使用(List<string> )转换对象,然后使用迭代

for (int i = 0; i < list.Count; i++) // Loop through List with for
        {
            Console.WriteLine(list[i]);
        }

You'll have to cast it back to a list of strings like so: 您必须将其强制转换为字符串列表,如下所示:

public void Method(object strList)
{
    List<string> internalStringList = strList as List<string>;
    //this is a save cast the "internalStringList" variable will 
    //be null if the cast fails.
}

You can also do this: 你也可以这样做:

List<string> internalStringList = (List<string>) strList;

but this could throw an InvalidCastException if strList isn't a List 但如果strList不是List,则可能抛出InvalidCastException

To be safe: 为了安全起见:

public void Method(object strList)
{
   var list = strList as List<string>;
   if (list != null)
    { 
       foreach(var item in list )
       {
          //code here
       }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM