标签:c class blog a tar http
方法一:应用ParameterizedThreadStart这个委托来传递输入参数,这种方法适用于传递单个参数的情况。
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
-
- namespace BeginInvokeTest
- {
-
-
-
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
-
- ParameterizedThreadStart start = new ParameterizedThreadStart(ChangeText);
- Thread thread = new Thread(start);
- object obj = "HelloWorld";
- thread.Start(obj);
- }
-
- public delegate void ChangeTextDelegate(object message);
-
- public void ChangeText(object message)
- {
-
-
- if (this.InvokeRequired)
- {
- this.BeginInvoke(new ChangeTextDelegate(ChangeText), message);
- }
- else
- {
- this.Text = message.ToString();
- }
- }
-
- }
- }
ParameterizedThreadStart 委托和 Thread.Start(Object)
方法重载使得将数据传递给线程过程变得简单,但由于可以将任何对象传递给
Thread.Start(Object),因此这种方法并不是类型安全的。将数据传递给线程过程的一个更可靠的方法是将线程过程和数据字段都放入辅助对
象。因此第一种方法是不推荐的。
方法二:利用线程实现类,将调用参数定义成属性的方式来操作线程参数,也就是将线程执行的方法和参数都封装到一个类里面。通过实例化该类,方法就可以调用属性来实现间接的类型安全地传递参数。通过之种方法可以传递多个参数。
- using System;
- using System.Threading;
-
-
-
-
- public class ThreadWithState {
-
- private string boilerplate;
- private int value;
-
-
- public ThreadWithState(string text, int number)
- {
- boilerplate = text;
- value = number;
- }
-
-
-
- public void ThreadProc()
- {
- Console.WriteLine(boilerplate, value);
- }
- }
-
-
-
- public class Example {
- public static void Main()
- {
-
- ThreadWithState tws = new ThreadWithState(
- "This report displays the number {0}.", 42);
-
-
-
- Thread t = new Thread(new ThreadStart(tws.ThreadProc));
- t.Start();
- Console.WriteLine("Main thread does some work, then waits.");
- t.Join();
- Console.WriteLine(
- "Independent task has completed; main thread ends.");
- }
- }
上面示例摘自MSDN
方法三:利用线程池来传递参数
方法四:利用匿名方法来传递参数,利用了匿名方法,连上面那种独立的类都省掉了,但是如果逻辑比较复杂,用这种方法就不太好了。
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
-
- namespace BeginInvokeTest
- {
- public partial class Form2 : Form
- {
- public Form2()
- {
- InitializeComponent();
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- Thread thread = new Thread(new ThreadStart(delegate()
- {
- this.BeginInvoke(new ChangeTextDelegate(ChangeText), "HelloWorld");
- }));
- thread.Start();
- }
-
- public delegate void ChangeTextDelegate(string message);
- public void ChangeText(string message)
- {
- this.Text = message;
- }
- }
- }
此外,如果需要从线程返回数据,这时可以用回调方法,下面示例摘自MSDN。
- using System;
- using System.Threading;
-
-
-
-
-
- public class ThreadWithState {
-
- private string boilerplate;
- private int value;
-
-
-
- private ExampleCallback callback;
-
-
-
- public ThreadWithState(string text, int number,
- ExampleCallback callbackDelegate)
- {
- boilerplate = text;
- value = number;
- callback = callbackDelegate;
- }
-
-
-
-
- public void ThreadProc()
- {
- Console.WriteLine(boilerplate, value);
- if (callback != null)
- callback(1);
- }
- }
-
-
-
- public delegate void ExampleCallback(int lineCount);
-
-
-
- public class Example
- {
- public static void Main()
- {
-
- ThreadWithState tws = new ThreadWithState(
- "This report displays the number {0}.",
- 42,
- new ExampleCallback(ResultCallback)
- );
-
- Thread t = new Thread(new ThreadStart(tws.ThreadProc));
- t.Start();
- Console.WriteLine("Main thread does some work, then waits.");
- t.Join();
- Console.WriteLine(
- "Independent task has completed; main thread ends.");
- }
-
-
-
-
- public static void ResultCallback(int lineCount)
- {
- Console.WriteLine(
- "Independent task printed {0} lines.", lineCount);
- }
- }
Net线程足迹 传递参数至线程,布布扣,bubuko.com
Net线程足迹 传递参数至线程
标签:c class blog a tar http
原文地址:http://www.cnblogs.com/happyday56/p/3762796.html