Net线程足迹 传递参数至线程

简介:

方法一:应用ParameterizedThreadStart这个委托来传递输入参数,这种方法适用于传递单个参数的情况。

[c-sharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Threading;  
  10.   
  11. namespace BeginInvokeTest  
  12. {  
  13.     /// <summary>  
  14.     /// 给线程传递参数  
  15.     /// </summary>  
  16.     public partial class Form1 : Form  
  17.     {  
  18.         public Form1()  
  19.         {  
  20.             InitializeComponent();  
  21.         }  
  22.   
  23.         private void button1_Click(object sender, EventArgs e)  
  24.         {  
  25.             //第一种方法,应用ParameterizedThreadStart这个委托来传递输入参数  
  26.             ParameterizedThreadStart start = new ParameterizedThreadStart(ChangeText);  
  27.             Thread thread = new Thread(start);  
  28.             object obj = "HelloWorld";  
  29.             thread.Start(obj);  
  30.         }  
  31.   
  32.         public delegate void ChangeTextDelegate(object message);  
  33.   
  34.         public void ChangeText(object message)  
  35.         {  
  36.             //InvokeRequired是Control的一个属性(这个属性是可以在其他线程里访问的)  
  37.             //这个属性表明调用方是否来自非UI线程,如果是,则使用BeginInvoke来调用这个函数,否则就直接调用,省去线程封送的过程  
  38.             if (this.InvokeRequired)  
  39.             {  
  40.                 this.BeginInvoke(new ChangeTextDelegate(ChangeText), message);  
  41.             }  
  42.             else  
  43.             {  
  44.                 this.Text = message.ToString();  
  45.             }  
  46.         }  
  47.   
  48.     }  
  49. }  

ParameterizedThreadStart 委托和 Thread.Start(Object) 方法重载使得将数据传递给线程过程变得简单,但由于可以将任何对象传递给 Thread.Start(Object),因此这种方法并不是类型安全的。将数据传递给线程过程的一个更可靠的方法是将线程过程和数据字段都放入辅助对 象。因此第一种方法是不推荐的。

 

方法二:利用线程实现类,将调用参数定义成属性的方式来操作线程参数,也就是将线程执行的方法和参数都封装到一个类里面。通过实例化该类,方法就可以调用属性来实现间接的类型安全地传递参数。通过之种方法可以传递多个参数。

 

[c-sharp]  view plain copy
  1. using System;  
  2. using System.Threading;  
  3.   
  4. // The ThreadWithState class contains the information needed for  
  5. // a task, and the method that executes the task.  
  6. //  
  7. public class ThreadWithState {  
  8.     // State information used in the task.  
  9.     private string boilerplate;  
  10.     private int value;  
  11.   
  12.     // The constructor obtains the state information.  
  13.     public ThreadWithState(string text, int number)   
  14.     {  
  15.         boilerplate = text;  
  16.         value = number;  
  17.     }  
  18.   
  19.     // The thread procedure performs the task, such as formatting   
  20.     // and printing a document.  
  21.     public void ThreadProc()   
  22.     {  
  23.         Console.WriteLine(boilerplate, value);   
  24.     }  
  25. }  
  26.   
  27. // Entry point for the example.  
  28. //  
  29. public class Example {  
  30.     public static void Main()   
  31.     {  
  32.         // Supply the state information required by the task.  
  33.         ThreadWithState tws = new ThreadWithState(  
  34.             "This report displays the number {0}.", 42);  
  35.   
  36.         // Create a thread to execute the task, and then  
  37.         // start the thread.  
  38.         Thread t = new Thread(new ThreadStart(tws.ThreadProc));  
  39.         t.Start();  
  40.         Console.WriteLine("Main thread does some work, then waits.");  
  41.         t.Join();  
  42.         Console.WriteLine(  
  43.             "Independent task has completed; main thread ends.");    
  44.     }  
  45. }  

上面示例摘自MSDN

 

方法三:利用线程池来传递参数

 

方法四:利用匿名方法来传递参数,利用了匿名方法,连上面那种独立的类都省掉了,但是如果逻辑比较复杂,用这种方法就不太好了。

 

[c-sharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Threading;  
  10.   
  11. namespace BeginInvokeTest  
  12. {  
  13.     public partial class Form2 : Form  
  14.     {  
  15.         public Form2()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.   
  20.         private void button1_Click(object sender, EventArgs e)  
  21.         {  
  22.             Thread thread = new Thread(new ThreadStart(delegate()  
  23.             {  
  24.                 this.BeginInvoke(new ChangeTextDelegate(ChangeText), "HelloWorld");  
  25.             }));  
  26.             thread.Start();  
  27.         }  
  28.   
  29.         public delegate void ChangeTextDelegate(string message);  
  30.         public void ChangeText(string message)  
  31.         {  
  32.             this.Text = message;  
  33.         }  
  34.     }  
  35. }  

 

此外,如果需要从线程返回数据,这时可以用回调方法,下面示例摘自MSDN。

[c-sharp]  view plain copy
    1. using System;  
    2. using System.Threading;  
    3.   
    4. // The ThreadWithState class contains the information needed for  
    5. // a task, the method that executes the task, and a delegate  
    6. // to call when the task is complete.  
    7. //  
    8. public class ThreadWithState {  
    9.     // State information used in the task.  
    10.     private string boilerplate;  
    11.     private int value;  
    12.   
    13.     // Delegate used to execute the callback method when the  
    14.     // task is complete.  
    15.     private ExampleCallback callback;  
    16.   
    17.     // The constructor obtains the state information and the  
    18.     // callback delegate.  
    19.     public ThreadWithState(string text, int number,   
    20.         ExampleCallback callbackDelegate)   
    21.     {  
    22.         boilerplate = text;  
    23.         value = number;  
    24.         callback = callbackDelegate;  
    25.     }  
    26.       
    27.     // The thread procedure performs the task, such as  
    28.     // formatting and printing a document, and then invokes  
    29.     // the callback delegate with the number of lines printed.  
    30.     public void ThreadProc()   
    31.     {  
    32.         Console.WriteLine(boilerplate, value);  
    33.         if (callback != null)  
    34.             callback(1);  
    35.     }  
    36. }  
    37.   
    38. // Delegate that defines the signature for the callback method.  
    39. //  
    40. public delegate void ExampleCallback(int lineCount);  
    41.   
    42. // Entry point for the example.  
    43. //  
    44. public class Example   
    45. {  
    46.     public static void Main()   
    47.     {  
    48.         // Supply the state information required by the task.  
    49.         ThreadWithState tws = new ThreadWithState(  
    50.             "This report displays the number {0}.",  
    51.             42,  
    52.             new ExampleCallback(ResultCallback)  
    53.         );  
    54.   
    55.         Thread t = new Thread(new ThreadStart(tws.ThreadProc));  
    56.         t.Start();  
    57.         Console.WriteLine("Main thread does some work, then waits.");  
    58.         t.Join();  
    59.         Console.WriteLine(  
    60.             "Independent task has completed; main thread ends.");   
    61.     }  
    62.   
    63.     // The callback method must match the signature of the  
    64.     // callback delegate.  
    65.     //  
    66.     public static void ResultCallback(int lineCount)   
    67.     {  
    68.         Console.WriteLine(  
    69.             "Independent task printed {0} lines.", lineCount);  
    70.     }  
    71. }  
分类:  基础知识

本文转自快乐就好博客园博客,原文链接:http://www.cnblogs.com/happyday56/p/3762796.html,如需转载请自行联系原作者
相关文章
|
9月前
|
并行计算 安全 Java
C# .NET面试系列四:多线程
<h2>多线程 #### 1. 根据线程安全的相关知识,分析以下代码,当调用 test 方法时 i > 10 时是否会引起死锁? 并简要说明理由。 ```c# public void test(int i) { lock(this) { if (i > 10) { i--; test(i); } } } ``` 在给定的代码中,不会发生死锁。死锁通常是由于两个或多个线程互相等待对方释放锁而无法继续执行的情况。在这个代码中,只有一个线程持有锁,且没有其他线程参与,因此不
473 3
|
3月前
|
开发框架 Java .NET
.net core 非阻塞的异步编程 及 线程调度过程
【11月更文挑战第12天】本文介绍了.NET Core中的非阻塞异步编程,包括其基本概念、实现方式及应用示例。通过`async`和`await`关键字,程序可在等待I/O操作时保持线程不被阻塞,提高性能。文章还详细说明了异步方法的基础示例、线程调度过程、延续任务机制、同步上下文的作用以及如何使用`Task.WhenAll`和`Task.WhenAny`处理多个异步任务的并发执行。
|
6月前
|
开发框架 前端开发 .NET
Asp.net Webapi 的 Post 方法不能把参数加到 URL 中?试试这样写
Asp.net Webapi 的 Post 方法不能把参数加到 URL 中?试试这样写
|
8月前
|
开发框架 监控 Java
【.NET Core】多线程之线程池(ThreadPool)详解(二)
【.NET Core】多线程之线程池(ThreadPool)详解(二)
130 3
|
8月前
|
SQL 开发框架 Java
【.NET Core】多线程之线程池(ThreadPool)详解(一)
【.NET Core】多线程之线程池(ThreadPool)详解(一)
510 2
|
8月前
|
算法 安全 Java
【.NET Core】 多线程之(Thread)详解
【.NET Core】 多线程之(Thread)详解
109 1
|
9月前
|
机器学习/深度学习 存储 编解码
多任务学习新篇章 | EMA-Net利用Cross-Task Affinity实现参数高效的高性能预测
多任务学习新篇章 | EMA-Net利用Cross-Task Affinity实现参数高效的高性能预测
239 0
|
9月前
|
程序员 数据库
VB.NET—Bug调试(参数话查询、附近语法错误)
VB.NET—Bug调试(参数话查询、附近语法错误)
82 0
|
前端开发 JavaScript
.net core 前端传递参数有值 后端接收到的数据却是null
1、问题分析 在做接口测试时,偶然出现了前端输出有值,但是后端断点调试时却出现接收参数总是为null的情况 2、解决办法 前端打印log,看前端的每一个传值的数据类型,与后端请求参数类进行认真的一一比对 小技巧: ① 直接打印调用接口的传参值的数据类型,例如 console.log(type of this.form.name) --string console.log(type of this.form.age) --number 打印的数据类型与后端接口的参数类比对,查出不对应的类型 ② 关于非必填的值,默认传值可能出现空字符串(' ')、NaN值(Not a Number
405 0
|
16天前
|
Linux
Linux编程: 在业务线程中注册和处理Linux信号
本文详细介绍了如何在Linux中通过在业务线程中注册和处理信号。我们讨论了信号的基本概念,并通过完整的代码示例展示了在业务线程中注册和处理信号的方法。通过正确地使用信号处理机制,可以提高程序的健壮性和响应能力。希望本文能帮助您更好地理解和应用Linux信号处理,提高开发效率和代码质量。
41 17

热门文章

最新文章