| 发表于:2007-01-13 08:51:392楼 得分:0 |
在创建操作系统进程时,操作系统将插入一个线程以执行该进程(包括任何原始应用程序域)中的代码。从此刻起,就可以创建和销毁应用程序域,而不必创建或销毁任何操作系统线程。如果正在执行的代码是托管代码,则可以通过在线程类 thread.currentthread 上检索静态属性来获取正在当前应用程序域中执行的线程的 thread 对象。 创建 thread 对象的新实例时,将创建新的托管线程。thread 的构造函数采用 threadstart 委托作为其唯一参数,该委托用于包装在调用 thread.start 时由新的 thread 调用的方法。多次调用 thread.start 将引发 threadstateexception。 thread.start 向系统提交异步请求,并且该调用可能在新的线程实际启动之前立即返回。可以使用 thread.threadstate 和 thread.isalive 在任一时刻确定线程的状态。thread.abort 中止线程,并对其进行标记以进行垃圾回收。下面的代码示例创建两个新线程以调用另一个对象上的实例和静态方法。 [c#] 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 threads. "); return 0; } } 向线程传递数据 threadstart 委托既没有参数也没有返回值。这意味着不可以使用需要参数的方法启动线程,或从方法中获得返回值。 为向线程传递数据,需要创建一个用来保持数据和线程方法的对象,如下面的两个代码示例所示。 为检索线程方法的结果,您可以使用回调方法,如第二个代码示例中所示。 [c#] 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. "); } } 用回调方法检索数据 下面的示例演示了一个从线程中检索数据的回调方法。包含数据和线程方法的类的构造函数也接受代表回调方法的委托;在线程方法结束前,它调用该回调委托。 [c#] 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); } } | | |
|