惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
B
Blog
Stack Overflow Blog
Stack Overflow Blog
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
MongoDB | Blog
MongoDB | Blog
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
Recent Announcements
Recent Announcements
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
D
DataBreaches.Net
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog
M
MIT News - Artificial intelligence
P
Proofpoint News Feed
罗磊的独立博客
L
LangChain Blog
V
Visual Studio Blog
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
宝玉的分享
宝玉的分享

博客园 - 费哥

VC++实现应用程序对插件的支持(转) typedef的四个用途和两个陷阱(转) c#动态创建ODBC数据源 设为首页,加入收藏,联系我们 - 费哥 - 博客园 ASP.NET 2.0中CSS失效 ADO.NET更新ACCESS碰到的怪异问题 DataGrid中的數據輸出到Excel 对 Windows 窗体控件进行线程安全调用 创建合理位置的线标注 Sql2005导出SQL2000格式的脚本 c#枚举的用法及遍历方法 将外部图块插入当前图形(c#代码) 无法将顶级控件添加到控件 使用Microsoft Visual Studio2005开发ObjectARX设置 CS1595:已在多处定义 软件设计师考试大纲 <<设计模式可复用面向对象软件的基础>>--组合模式(Composite) <<设计模式可复用面向对象软件的基础>>--设计模式怎样解决设计问题 <<设计模式可复用面向对象软件的基础>>读书笔记--(第一章)引言
将参数传递给线程(Vc#2005)
费哥 · 2008-04-08 · via 博客园 - 费哥

根据MSDN

在 .NET Framework 2.0 版中,ParameterizedThreadStart 委托提供了一种简便方法,可以在调用

System.Threading.Thread.Start(System.Object)

方法重载时将包含数据的对象传递给线程。
使用 ParameterizedThreadStart 委托不是传递数据的类型安全的方法,因为 System.Threading.Thread.Start(System.Object) 方法重载接受任何对象。一种替代方法是将线程过程和数据封装在帮助器类中,并使用 ThreadStart 委托执行线程过程。

在简单情况下传递参数给线程有两种方法,一种是使用ParameterizedThreadStart 直接传递Object类型的参数。这种传递时非线程安全的。另一种方法是使用间接传递,我们可以把把数据和线程过程封装到帮助器类中或者直接在另一个不带参数的函数中调用线程过程并传递所需的参数。

使用ParameterizedThreadStart 的示例如下:

 1 class Program
 2     {
 3         static Thread thread;
 4         static void Main(string[] args)
 5         {
 6             thread = new Thread(new ParameterizedThreadStart(ThreadProc));
 7             thread.Start(1000);
 8         }
 9 
10         static void ThreadProc(object max)
11         {
12             for (int i = 0; i < (int)max; i++)
13             {
14                 Console.WriteLine(i);
15             }
16             thread.Abort();
17         }
18     }

使用函数传递参数:

class Program
    {
        
static Thread thread;
        
static void Main(string[] args)
        {
            thread 
= new Thread(new ThreadStart(Test));
            thread.Start();
        }
static void Test()
        {
            ThreadProc(
1000);
        }
static void ThreadProc(int max)
        {
            
for (int i = 0; i < max; i++)
            {
                Console.WriteLine(i);
            }
            thread.Abort();
        }
    }

使用类帮助器通过构造函数传递参数:

 1 public class Program
 2     {
 3         public static Thread thread;
 4         static void Main(string[] args)
 5         {
 6             ThreadTest tt = new ThreadTest(1000);
 7             thread = new Thread(new ThreadStart(tt.ThreadProc));
 8             thread.Start();
 9         }
10     }
11 
12     public class ThreadTest
13     {
14         int max;
15         public ThreadTest(int pMax)
16         {
17             max = pMax;
18         }
19 
20         public void ThreadProc()
21         {
22             for (int i = 0; i < max; i++)
23             {
24                 Console.WriteLine(i);
25             }
26             Program.thread.Abort();
27         }
28     }