传递参数给线程 [英] passing parameters to a thread

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

问题描述

我想通过一个函数,一个参数的构造函数的ThreadStart在C#中。但是,看来这是不可能的,因为我得到一个语法错误时我尝试做这样的事情。

I want to pass a function that takes a parameter to the ThreadStart Constructor in C#. But, it seems that this is not possible, as I get a syntax error it I try to do something like this

Thread t1 = new Thread(new ThreadStart(func1(obj1));

其中OBJ1是类型的对象列表<字符串方式> (说)

where obj1 is an object of type List<string> (say).

如果我想要一个线程来执行此功能,在一个对象需要为一个参数,我打算同时创建2个这样的线程有不同的参数值是什么,是实现这一目标的最佳方法是什么?

If I want a thread to execute this function that takes in an object as a parameter, and I plan to create 2 such threads simultaneously with different parameter values what is the best method to achieve this?

推荐答案

您需要 ParametrizedThreadStart 要传递参数给线程

You need ParametrizedThreadStart to pass a parameter to a thread.

Thread t1 = new Thread(new ParametrizedThreadStart(func1);
t1.Start(obj1);

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

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