你说的是这个吧...public Thread (ParameterizedThreadStart start)...给你个例子吧: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' */
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."); } }
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'
*/
Thread newThread =new Thread(,,,)
我记得最后的参数是objedt,好就没有用了.
---------------------------------------------------------------------
我在2003中没有找到这个委托:ParameterizedThreadStart
它是C#定义还是自定义的?
我没用过2005,不知道这个传参的Start方法是不是可靠。
它是C#定义还是自定义的?
我没用过2005,不知道这个传参的Start方法是不是可靠。C#定义的...这个我也没怎么用过,我一般都用不传参的ThreadStart...
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.");
}
}
zyip给大家讲一个世界上最简单也最复杂的方法,匿名委托:
Thread t = new Thread(delegate() { proc(10,obj); });
t.Start();private void proc(int a,ref obj)
{
......
}这的用法很简单,理论比较复杂,照用就行
t.Start();private void proc(int a,ref obj)
{
......
}错了,刚才少了个ref