| 发表于:2007-02-08 15:50:32 楼主 |
这个问题已经困扰我好久了,请各位高手帮忙,先谢谢大家了 我的线程是单独放在一个类里面,通过创建这个类的对象来创建线程,本来想用锁来控制线程,但是线程是在不同对象里的,所以没法控制,用lock,mutex,monitor都没用,但现在又遇到多线程写同一个文件这个问题,真是这个问题搞死了, 我的结构如下: class a { .......... private void bt_select_click(object sender, system.eventargs e) { .... for(int i=0;i <xmllist.count;i++) { b b=new b(xmllist[i],i); } .... } .......... } class b { ...... public b(string ip,string i) { thread thd = new thread(new threadstart(excuteimport)); j=i+1; thd.name= "线程 "+j+ ",支队ip: "+ip; sb_log.append( "/*******************************/ "); sb_log.append( "【 "+thd.name+ "】 "); thd.start(); } ....... private void writefilelog() { datetime dt=datetime.now; filestream fs; string path=directory.getcurrentdirectory()+ "\\log\\ "; string filename=dt.tostring( "d ")+ ".log "; streamwriter writefilelog; if(!directory.exists(path)) { directory.createdirectory(path); } fs=new filestream(path+filename,filemode.openorcreate,fileaccess.write,fileshare.readwrite); writefilelog=new streamwriter(fs); writefilelog.write(sb_log); writefilelog.close(); fs.close(); } } |
|
|
|
|