怎样往线程函数传递参数,急,谢谢
如题,能不能举个例子?谢谢 --------------------编程问答-------------------- up --------------------编程问答-------------------- 使用类变量
--------------------编程问答-------------------- up --------------------编程问答-------------------- 类变量 怎么用? --------------------编程问答-------------------- 线程不会在创建时开始执行。若要为执行而调度线程,请调用 Start 方法。若要将数据对象传递给线程,请使用 Start(Object) 方法重载。
using System;
using System.Threading;
public class Work
{
public static void Main()
{
// To start a thread using a shared thread procedure, use
// the class name and method name when you create the
// ParameterizedThreadStart delegate.
//
Thread newThread = new Thread(
new ParameterizedThreadStart(Work.DoWork));
// Use the overload of the Start method that has a
// parameter of type Object. You can create an object that
// contains several pieces of data, or you can pass any
// reference type or value type. The following code passes
// the integer value 42.
//
newThread.Start(42);
// To start a thread using an instance method for the thread
// procedure, use the instance variable and method name when
// you create the ParameterizedThreadStart delegate.
//
Work w = new Work();
newThread = new Thread(
new ParameterizedThreadStart(w.DoMoreWork));
// Pass an object containing data for the thread.
//
newThread.Start("The answer.");
}
public static void DoWork(object data)
{
Console.WriteLine("Static thread procedure. Data='{0}'",
data);
}
public void DoMoreWork(object data)
{
Console.WriteLine("Instance thread procedure. Data='{0}'",
data);
}
}
/* This code example produces the following output (the order
of the lines might vary):
Static thread procedure. Data='42'
Instance thread procedure. Data='The answer'
*/
MSDN上示例 --------------------编程问答-------------------- 用异步委托创建线程可以给线程函数直接传参数
public delegate void CommunicationHandler(string[,] sReceiveData);//声明委托类型,注意签名
public void SlaveDataProcessEnd(string[,] sReceiveData)//调用异步委托的函数,可以是某一事件的触发函数
{
this.BeginInvoke(new CommunicationHandler(SetValueHandler), new Object[]{sReceiveData });//可以传参数 new Object[]{sReceiveData }
}
public void SetValueHandler(string[,] sReceiveData)//异步委托的执行函数
{
} --------------------编程问答-------------------- 谢谢
--------------------编程问答-------------------- using System;
using System.Threading;
// The ThreadWithState class contains the information needed for
// a task, and the method that executes the task.
//
public class ThreadWithState {
// State information used in the task.
private string boilerplate;
private int value;
// The constructor obtains the state information.
public ThreadWithState(string text, int number)
{
boilerplate = text;
value = number;
}
// The thread procedure performs the task, such as formatting
// and printing a document.
public void ThreadProc()
{
Console.WriteLine(boilerplate, value);
}
}
// Entry point for the example.
//
public class Example {
public static void Main()
{
// Supply the state information required by the task.
ThreadWithState tws = new ThreadWithState(
"This report displays the number {0}.", 42);
// Create a thread to execute the task, and then
// start the thread.
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.");
}
} --------------------编程问答-------------------- 使用回调方法检索数据
下面的示例演示了一个从线程中检索数据的回调方法。包含数据和线程方法的类的构造函数也接受代表回调方法的委托;在线程方法结束前,它调用该回调委托。
using System;
using System.Threading;
// The ThreadWithState class contains the information needed for
// a task, the method that executes the task, and a delegate
// to call when the task is complete.
//
public class ThreadWithState {
// State information used in the task.
private string boilerplate;
private int value;
// Delegate used to execute the callback method when the
// task is complete.
private ExampleCallback callback;
// The constructor obtains the state information and the
// callback delegate.
public ThreadWithState(string text, int number,
ExampleCallback callbackDelegate)
{
boilerplate = text;
value = number;
callback = callbackDelegate;
}
// The thread procedure performs the task, such as
// formatting and printing a document, and then invokes
// the callback delegate with the number of lines printed.
public void ThreadProc()
{
Console.WriteLine(boilerplate, value);
if (callback != null)
callback(1);
}
}
// Delegate that defines the signature for the callback method.
//
public delegate void ExampleCallback(int lineCount);
// Entry point for the example.
//
public class Example
{
public static void Main()
{
// Supply the state information required by the task.
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.");
}
// The callback method must match the signature of the
// callback delegate.
//
public static void ResultCallback(int lineCount)
{
Console.WriteLine(
"Independent task printed {0} lines.", lineCount);
}
} --------------------编程问答-------------------- 在创建操作系统进程时,操作系统将插入一个线程以执行该进程(包括任何原始应用程序域)中的代码。从此刻起,就可以创建和销毁应用程序域,而不必创建或销毁任何操作系统线程。如果正在执行的代码是托管代码,则可以通过检索类型为 Thread 的静态 CurrentThread 属性来获取正在当前应用程序域中执行的线程的 Thread 对象。本主题描述线程创建,并讨论用于向线程过程传递数据的替代方法。
创建线程
创建新的 Thread 对象时,将创建新的托管线程。Thread 类具有接受一个 ThreadStart 委托或 ParameterizedThreadStart 委托的构造函数:该委托包装调用 Start 方法时由新线程调用的方法。多次调用 Start 将引发 ThreadStateException。
Start 方法立即返回,经常是在实际启动新线程之前。可以使用 ThreadState 和 IsAlive 属性确定任何时刻的线程状态,但是绝不应该将这些属性用于同步线程活动。下面的代码示例创建两个新线程以调用另一个对象上的实例和静态方法。
using System;
using System.Threading;
public class ServerClass
{
// The method that will be called when the thread is started.
public void InstanceMethod()
{
Console.WriteLine(
"ServerClass.InstanceMethod is running on another thread.");
// Pause for a moment to provide a delay to make
// threads more apparent.
Thread.Sleep(3000);
Console.WriteLine(
"The instance method called by the worker thread has ended.");
}
public static void StaticMethod()
{
Console.WriteLine(
"ServerClass.StaticMethod is running on another thread.");
// Pause for a moment to provide a delay to make
// threads more apparent.
Thread.Sleep(5000);
Console.WriteLine(
"The static method called by the worker thread has ended.");
}
}
public class Simple{
public static int Main(String[] args)
{
Console.WriteLine("Thread Simple Sample");
ServerClass serverObject = new ServerClass();
// Create the thread object, passing in the
// serverObject.InstanceMethod method using a
// ThreadStart delegate.
Thread InstanceCaller = new Thread(
new ThreadStart(serverObject.InstanceMethod));
// Start the thread.
InstanceCaller.Start();
Console.WriteLine("The Main() thread calls this after "
+ "starting the new InstanceCaller thread.");
// Create the thread object, passing in the
// serverObject.StaticMethod method using a
// ThreadStart delegate.
Thread StaticCaller = new Thread(
new ThreadStart(ServerClass.StaticMethod));
// Start the thread.
StaticCaller.Start();
Console.WriteLine("The Main() thread calls this after "
+ "starting the new StaticCaller thread.");
return 0;
}
} --------------------编程问答-------------------- 在 .NET Framework 2.0 版中,ParameterizedThreadStart 委托提供了一种简便方法,可以在调用 System.Threading.Thread.Start(System.Object) 方法重载时将包含数据的对象传递给线程。有关代码示例,请参见 ParameterizedThreadStart。
使用 ParameterizedThreadStart 委托不是传递数据的类型安全的方法,因为 System.Threading.Thread.Start(System.Object) 方法重载接受任何对象。一种替代方法是将线程过程和数据封装在帮助器类中,并使用 ThreadStart 委托执行线程过程。该技术在下面的两个代码示例中演示。
这两个委托都没有返回值,因为没有地方用于从异步调用中返回数据。为检索线程方法的结果,您可以使用回调方法,如第二个代码示例中所示。
using System;
using System.Threading;
// The ThreadWithState class contains the information needed for
// a task, and the method that executes the task.
//
public class ThreadWithState {
// State information used in the task.
private string boilerplate;
private int value;
// The constructor obtains the state information.
public ThreadWithState(string text, int number)
{
boilerplate = text;
value = number;
}
// The thread procedure performs the task, such as formatting
// and printing a document.
public void ThreadProc()
{
Console.WriteLine(boilerplate, value);
}
}
// Entry point for the example.
//
public class Example {
public static void Main()
{
// Supply the state information required by the task.
ThreadWithState tws = new ThreadWithState(
"This report displays the number {0}.", 42);
// Create a thread to execute the task, and then
// start the thread.
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.");
}
}
补充:.NET技术 , C#