#眉標=SD #副標= Microsoft.NET Design Patterns專欄 #大標=非同步程式設計模式 #作者=文/林耀珍 -----box----- #程式1 // Insert BatchRequest transaction = RequestDBOp.InsertRequestDetails(..) // Insert each service request in the batch for(i=0 ; i < listCount; i++) { RequestDBOp.InsertServiceAgentDetails(transaction,) } RequestDBOp.CommitRequestInsertion(transaction); -----end----- -----box----- #程式2 // Get stored Procedure Name storedProcedureName = AsyncResources.GetConstantValue ("SP_INSERT_SA_DETAILS"); // Create sqlParameters, and set it's value sqlParams[sqlParamCounter]=new SqlParameter...; … SqlHelper.ExecuteNonQuery(…); -----end----- -----box----- 程式3 // In worker thread ThreadRoutine() { while(true) { lock (threadPool) if this is a new started thread while (request queue is empty) Monitor.Wait(threadPool, …); get a request by RequestQueue.Dequque(); process this request; } } // In AsyncProcessor ExecuteServiceAgent() { While(true) { Sleep(reCheckTimeInterval); Retrieve requestBatch; Foreach (ServiceAgentRequest sa in batch) ThreadPool. AddServiceAgent(ReqDetails); } } // In thread pool object AddServiceAgent(ReqDetails) { RequestQueue.Enqueue(ReqDetails); Monitor.Pulse(); } -----end----- -----box----- #程式4 // In each Worker thread ThreadRoutine() { Wait request; get a request by RequestQueue.Dequque(); create service agent from according to request details serviceAgentRequest.UpdateFinishTime(); Monitor.AddServiceAgent(..); Execute service agent; Monitor.RemoveServiceAgent(); } // In Monitor's TimerProcedure TimerProcedure(object asyncThreadPool) { Iterate thru each monitored thread, { If (Request > TTL) stop this workthread, supply a new workthread; if (now > FinishTime & retry count > 0) decrement retry count, stop this workthread, supply a new workthread with retried request; if (now > FinishTime & retry count == 0) update status to failed, // to be collected stop this workthread, supply a new workthread into pool } } -----end-----