您的位置:程序门 -> .net技术 -> c#



多线程问题?求教


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


多线程问题?求教
发表于:2007-01-22 11:41:20 楼主
这里是邵志东老师在微软讲的一堂asp.net多线程课的代码,数组排序代码:
不知道有人看过其代码没?我的问题在下面有标识,恳请高手们赐教!
private   void   btnsort_click(object   sender,   system.eventargs   e)
{
btnsort.enabled   =   false;
valuearray   =   new   int[convert.toint32(ddlnum.selecteditem.text.trim())];
threadholder.clear();
valuearray.initialize();
threadcounter   =   0;
lbmsg.text   =   "排序进行中... ";
/*   insert   value   in   to   the   valuearray   */
for(int   i=0;   i <valuearray.length;i++)
{
valuearray[i]   =   valuearray.length-i;
//valuearray[i]   =   convert.toint32(randomnumber.next(1000));
}
/* start   a   timer   to   check   the   time   to   sort   the   array   */
starttime   =   datetime.now;
/*   start   threads   to   sort   the   values   in   the   arry   */
for(int   t=0;   t <   convert.toint32(ddlthreadnum.selecteditem.text.trim());t++)
{
thread   thread   =   new   thread(new   threadstart(sort));
thread.name   =   convert.tostring(t);
thread.start();
}
//我的问题是:这段代码没有等待线程返回,为什么呢?会不会其他线程还没有执行完而这主线程已经进行完了???不解啊!
page.registerstartupscript( " ", " <script> window.settimeout( 'location.href=location.href ',5000); </script> ");

}
public   void   sort()
{
try
{
while(true)
{
swaped   =   false;
for   (int   j   =   0;   j <valuearray.length-1;   j++)
{
lock(typeof(thread))
{ /*   if   the   left-hand   side   value   is   greater   swap   values*/
if(valuearray[j]   >   valuearray[j+1])  
{
int   t   =   valuearray[j];
valuearray[j]   =   valuearray[j+1];
valuearray[j+1]   =   t;
swaped   =   true;
}
}
}
thread.sleep(1);
if(!swaped)   {   break;   }
}
thread.currentthread.abort();
}
catch(exception   ex)
{
if(   interlocked.increment(ref   threadcounter)   ==   convert.toint64(ddlthreadnum.selecteditem.text.tostring().trim()))
display();
}
}
发表于:2007-01-22 11:54:441楼 得分:0
lz再看看“多线程”吧
发表于:2007-01-22 13:17:542楼 得分:0
to://我的问题是:这段代码没有等待线程返回,为什么呢?会不会其他线程还没有执行完而这主线程已经进行完了???不解啊!

你这里是主线程,你一个for循环,启动了几个子线程,那么子线程就会执行子线程自己的,和主线程执行互不影响...主线程创建并启动完几个新创建的子线程后,继续往下执行...

发表于:2007-01-22 13:26:543楼 得分:0
晕~


快速检索

最新资讯
热门点击