Address: http://www.cnblogs.com/sandy_liao/archive/2010/11/29/1891533.html
in the development of often encounter threading example, if a background operation to compare the time, we can start a thread to carry out the lengthy operation, while the program continues to execute. In some cases, there may be problems with multiple threads of synchronization, and the following example shows how the two threads work together. The idea of this program is to do one thing together (delete an element from a ArrayList), and if execution is done, two threads will stop executing. The code is as follows:usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Threading;/// <summary>///in the development of often encounter threading example, if a background operation to compare the time, we can start a thread to carry out the lengthy operation, while the program continues to execute. In some cases, there may be problems with multiple threads of synchronization, and the following example shows how the two threads work together. //////The idea of this program is to do one thing together (delete an element from a ArrayList), and if execution is done, two threads will stop executing. ///Zhou Gong///Time: 2008-5-17///Original Address:HTTP://BLOG.CSDN.NET/ZHOUFOXCN/// </summary> Public classthreaddemo{Privatethread[] Threads; Private intthrs =5; PrivateArrayList stringlist; Private EventEventHandler onnumberclear;//events raised by data delete completion Public Static voidMain () {Threaddemo demo=NewThreaddemo ( -); Demo. Action (); } PublicThreaddemo (intNumber ) {Random random=NewRandom (1000000); Stringlist=NewArrayList (number); for(inti =0; I < number; i++) {Stringlist.add (i.tostring ()); } Threads=Newthread[thrs]; for(inti =0; i < thrs; i++) {Threads[i]=NewThread (NewThreadStart (Run)); Threads[i]. Name="Threads"+ (i +1); } onnumberclear+=NewEventHandler (threaddemo_onnumberclear); } /// <summary> ///Start Working/// </summary> Public voidAction () { for(inti =0; i < thrs; i++) {Threads[i]. Start (); } } /// <summary> ///work done together./// </summary> Private voidRun () {stringStringValue =NULL; while(true) {Monitor.Enter ( This);//lock, keep in syncStringValue = (string) stringlist[0]; Console.WriteLine (Thread.CurrentThread.Name+"removed the"+stringvalue); Stringlist.removeat (0);//Delete an element in a ArrayList if(Stringlist.count = =0) {onnumberclear ( This,NewEventArgs ());//Raise Completion Event} monitor.exit ( This);//Cancel LockThread.Sleep (5); } } //when execution is complete, stop all threads voidThreaddemo_onnumberclear (Objectsender, EventArgs e) {Console.WriteLine ("execution is finished and all threads are stopped. "); for(inti =0; i < thrs; i++) {Threads[i]. Abort (); } }}
Case:
usingPMS. dbentity;usingSystem;usingsystem.web;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.IO;namespacePMS. convert.pdf{ Public structFileInfo { Public intFileid; Public stringfilepath; } Public classconvertswf {Staticqueue<fileinfo> filelist =NewQueue<fileinfo>(); Private Static stringRootPath = HttpContext.Current.Server.MapPath ("~/"); Private StaticThread T =NULL; Private Static stringconn =""; Public Static voidLoadqueue (intClassIDstring_conn) {Conn=_conn; using(varentity =Newpmsentities (conn)) {dalcontent dal=Newdalcontent (); varFileInfo = ( fromPinchentity.jcms_normal_contentwhereP.classid = = ClassId && P.ispass = =1&& p.customfield05.substring (0,Ten) =="PDF"&& P.CUSTOMFIELD03! =NULL Selectp); if(!FileInfo. Any ()) {return; } foreach(varIteminchFileInfo) {filelist. Enqueue (NewFileInfo {Fileid = Item. Id, filepath = RootPath +""+ Item. Customfield03.remove (0,1) }); Item. CUSTOMFIELD05="Convet"; } entity. SaveChanges (); } } Public Static voidExectreade () {Try { if(T = =NULL) {T=NewThread (Run); T.start (); } if(!t.isalive) {T=NewThread (Run); T.start (); } } Catch(Exception) {Throw; } } Public Static stringSourceFile =""; Static stringoutfile =""; Static voidRun () {pdf2swf pdf2swf=Newpdf2swf (); stringSavename =""; stringExtension =""; stringoutfile =""; intID =0; using(varentity =Newpmsentities (conn)) { while(FileList. Count >0) {FileInfo F=filelist. Dequeue (); SourceFile=F.filepath; ID=F.fileid; Savename=Path.getfilename (sourcefile); Extension=path.getextension (sourcefile). ToLower (); Savename= Savename.substring (0, Savename.length-extension. Length); OutFile= Path.getdirectoryname (sourcefile) +"/"+ Savename +"_%.swf"; Try{pdf2swf. Pdfconverttoswf (SourceFile, outfile); } Catch(Exception ex) {Throwex; Continue; } varContent = Entity.jcms_normal_content. Singleordefault (p = p.id = =ID); if(File.exists (outfile. Replace ("_%","_1")) {content. CUSTOMFIELD04="/"+ outfile. Replace (RootPath,""). Replace ("_%","_[*,0]"); Content. CUSTOMFIELD05=" Complete"; Entity. SaveChanges (); } Else{content. CUSTOMFIELD04=""; Content. CUSTOMFIELD05="Error"; Entity. SaveChanges (); }}} t.abort (); } }}
Making wheels: The small use of threads