/// <summary> ///multithreaded Execution/// </summary> Public classMultithreadingworker {/// <summary> ///make sure that all threads are complete/// </summary> PrivateDone M_done =NULL; Private LongM_starttime =0; PrivateQueue m_workload =NULL; PublicMultithreadingworker () {M_done=NewDone (); M_workload=NewQueue (); } /// <summary> ///content Delegation for thread work/// </summary> PublicWorkcontent Workcontent {Get;Set; } /// <summary> ///Number of Threads/// </summary> Public intThreadCount {Get;Set; } Private BOOLM_quit; Public BOOLQuit {Get{returnM_quit;} Set{M_quit =value;} } /// <summary> ///increase the working object to the queue/// </summary> /// <param name= "work" ></param> Public voidaddwork (Object work) {Monitor.Enter ( This); M_workload. Enqueue (work); Monitor.pulse ( This); Monitor.Exit ( This); } /// <summary> ///fetching objects from the queue/// </summary> /// <returns></returns> PrivateObject obtainwork () {Monitor.Enter ( This); while(M_workload. Count <1) {monitor.wait ( This); } Object Next=M_workload. Dequeue (); Monitor.Exit ( This); returnNext; } /// <summary> ///Start Execution/// </summary> Public voidStart () {m_starttime=System.DateTime.Now.Ticks; M_done. Reset (); M_quit=false; for(inti =1; I <= threadcount; i++) {ThreadStart TS=NewThreadStart ( This. Process); Thread M_thread=NewThread (TS); M_thread. Start (); } m_done. Waitbegin (); M_done. Waitdone (); } /// <summary> ///Stop Execution/// </summary> Public voidStop () {m_quit=true; } Private voidProcess () { while(m_quit==false) {Object workobj=obtainwork (); M_done. Workerbegin (); Try{workcontent (workobj); } Catch(Exception e) {System.Console.WriteLine ("Error:"+e.message); } finally{M_done. Workerend (); } } } } /// <summary> ///execute content applicable delegate/// </summary> /// <param name= "work" ></param> Public Delegate voidworkcontent (Object work); Public classDone {Private intM_activethreads =0; Private BOOLm_started =false; Public voidWaitdone () {Monitor.Enter ( This); while(m_activethreads>0) {monitor.wait ( This); } monitor.exit ( This); } Public voidWaitbegin () {Monitor.Enter ( This); while( !m_started) {monitor.wait ( This); } monitor.exit ( This); } /// <summary> ///Execution Start/// </summary> Public voidWorkerbegin () {Monitor.Enter ( This); M_activethreads++; M_started=true; Monitor.pulse ( This); Monitor.Exit ( This); } /// <summary> ///execution Complete/// </summary> Public voidWorkerend () {Monitor.Enter ( This); M_activethreads--; Monitor.pulse ( This); Monitor.Exit ( This); } /// <summary> ///re-initialize/// </summary> Public voidReset () {Monitor.Enter ( This); M_activethreads=0; Monitor.Exit ( This); } }
Calling code:
Public classtestmultithreading { Public Static voidTest () {Multithreadingworker thwork=NewMultithreadingworker (); Thwork.threadcount= -; Thwork.workcontent=Newworkcontent (Workfun); for(inti =0; I < -; i++) thwork.addwork (i); Thwork.start (); } Private Static voidWorkfun (Objectobj) {Console.WriteLine ("What to do:"+obj. ToString ()); } }
C # Multithreading Management code