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,如需转载请自行联系原作者
相关文章
|
2月前
|
机器学习/深度学习 存储 编解码
多任务学习新篇章 | EMA-Net利用Cross-Task Affinity实现参数高效的高性能预测
多任务学习新篇章 | EMA-Net利用Cross-Task Affinity实现参数高效的高性能预测
42 0
|
4月前
|
程序员 数据库
VB.NET—Bug调试(参数话查询、附近语法错误)
VB.NET—Bug调试(参数话查询、附近语法错误)
25 0
|
6月前
|
前端开发 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
97 0
|
9月前
|
前端开发
解决.NET Core Ajax请求后台传送参数过大请求失败问题
解决.NET Core Ajax请求后台传送参数过大请求失败问题
|
前端开发
.NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
.NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
371 0
.NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
.NET中将集合M内非空参数值的参数按照参数名ASCII码从小到大排序(字典序),并使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串stringA
.NET中将集合M内非空参数值的参数按照参数名ASCII码从小到大排序(字典序),并使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串stringA
226 0
.Net ADO拼接带参数的SQL语句
.Net ADO拼接带参数的SQL语句
118 0
|
开发框架 前端开发 JavaScript
ASP.NET 母版页,内容页之间如何传参数
绪论:本文介绍如何使用.net 母版页(.master)和内容页(.aspx)相互传参数
72 0
ASP.NET 母版页,内容页之间如何传参数
|
C# Windows
.NET一个线程更新另一个线程的UI(两种实现方法及若干简化)
原文:.NET一个线程更新另一个线程的UI(两种实现方法及若干简化) 本片博文接上一篇:.NET多线程执行函数,给出实现一个线程更新另一个线程UI的两种方法。 Winform中的控件是绑定到特定的线程的(一般是主线程),这意味着从另一个线程更新主线程的控件不能直接调用该控件的成员。
1304 0
|
开发框架 .NET
面试官:如何在ASP.NET Core里给Action传递参数
在ASP.NET Core 中给Action传参是在面试和实际开发中必定会遇到和使用到的,那么下面我们就来说说如何给Action传参。常用的传参方式有四种: 1. url 方式; 2. querystring 方式; 3. request header 方式; 4. request body 方式。
237 0