. NET提供了多种创建和启动线程的方法。 那么有两种方法(基本上)。
(1)使用System.Threading.Thread类。
thread curr=new thread (my function; curr.start (新对象) ); )2)使用System.Threading.ThreadPool类。
1 .无参数
thread pool.queueuserworkitem (delegate (postdatawhenloaddata ); );
私有语音下载数据(
{
behaviorreturnbr=_ re service.searchprintreport (currentso,isCount );
if(br.Success ) )。
{
sysnassignwhenload (br.object list;
if(br.objectlist.count==0) )。
{
var Sr msg=new服务器消息
{
代码=' no data ',
消息cn='没有符合查询条件的数据'
(;
br.messagelist.add(Srmsg;
sysnshowerror(br;
}
}
else
{
sysnshowerror(br;
}
}
2 .带参数
string ti=' ';
thread pool.queueuserworkitem (newwaitcallback ) threadproc ),ti ); (参数ti可以是任意类型)
私有语音(对象ti )//回调函数的参数类型必须写为对象
{
}
总结:
创建线程需要很大的开销。 Thread创建新线程,ThreadPool通过从线程池中重用挂起的空闲线程来减少线程创建开销。 使用ThreadPool线程时,这些线程由系统管理。 如果要多次执行小任务,线程池更适合。