C#中的两个对象类不工作线程 [英] c# two object in class dont work thread

查看:94
本文介绍了C#中的两个对象类不工作线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的构造:

private readonly List<Thread> thr = new List<Thread>();

在一个类中,我有一个带有一个要调用线程的参数的方法.

In a class i have a method with one parameter that i want to call threaded.

    public void testthr(object xxx)
    {
     ......
    }

在按钮上单击我启动线程"

on button click i start a thread

        for (Int32 i = 0; i < textBox8.Lines.Length; i++)
        {

            var thr1 = new Thread(testthr);
            thr1.Start(textBox8.Lines[i].Trim());

            thr.Add(threadz);

        }

如何使一个线程具有多个参数?喜欢:

How to make a thread with more than one parameter? Like:

    public void testthr(object xxx, string yyy)
    {
     ......
    }

该类在线程启动吗?

推荐答案

如果要将多个值传递给线程proc,则需要创建一个对象来包含它们.有几种方法可以做到这一点.最简单的方法可能是使用元组:

If you want to pass multiple values to a thread proc, you need to create an object to contain them. There are several ways to do that. The easiest is probably to use a Tuple:

for (Int32 i = 0; i < textBox8.Lines.Length; i++)
{

    var thr1 = new Thread(testthr);
    var data = new Tuple<string, string>(textBox8.Lines[i].Trim(), "hello");
    thr1.Start(data);

    thr.Add(thr1);

}

public void testthr(object state)
{
    var data = (Tuple<string,string>)state;
    var item1 = data.Item1;
    var item2 = data.Item2;
    ...
}

这篇关于C#中的两个对象类不工作线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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