程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 並發事件: 通過C#簡化APM(2)

並發事件: 通過C#簡化APM(2)

編輯:關於C語言

Figure1ApmPatterns 類的摘錄

private static void SynchronousPattern() {
   using (FileStream fs =
     new FileStream(typeof(ApmPatterns).Assembly.Location,
     FileMode.Open, FileAccess.Read, FileShare.Read, 8192,
     FileOptions.Asynchronous)) {
   Byte[] data = new Byte[fs.Length];
   Int32 bytesRead = fs.Read(data, 0, data.Length);
   ProcessData(data);
   }
}
#region APM Pattern with Multiple Methods
private sealed class ApmData {
   public FileStream m_fs;
   public Byte[] m_data;
}
private static void ApmPatternWithMultipleMethods() {
   ApmData apmData = new ApmData();
   apmData.m_fs =
     new FileStream(typeof(ApmPatterns).Assembly.Location,
     FileMode.Open, FileAccess.Read, FileShare.Read, 8192,
     FileOptions.Asynchronous);
   apmData.m_data = new Byte[apmData.m_fs.Length];
   apmData.m_fs.BeginRead(apmData.m_data, 0, apmData.m_data.Length,
     ReadCompleted, apmData);
   // Method returns after issuing async I/O request
}
private static void ReadCompleted(IAsyncResult result) {
   ApmData apmData = (ApmData) result.AsyncState;
   Int32 bytesRead = apmData.m_fs.EndRead(result);
   ProcessData(apmData.m_data);
   apmData.m_fs.Close(); // No 'using'
}
#endregion
private static void ApmPatternWithAnonymousMethod() {
   FileStream fs =
     new FileStream(typeof(ApmPatterns).Assembly.Location,
     FileMode.Open, FileAccess.Read, FileShare.Read, 8192,
     FileOptions.Asynchronous);
   Byte[] data = new Byte[fs.Length];
   fs.BeginRead(data, 0, data.Length, delegate(IAsyncResult result) {
     Int32 bytesRead = fs.EndRead(result);
     ProcessData(data);
     fs.Close(); // No 'using'
   }, null);
   // Method returns after issuing async I/O request
}
private static void ApmPatternWithLambdaExpression() {
   FileStream fs =
     new FileStream(typeof(ApmPatterns).Assembly.Location,
     FileMode.Open, FileAccess.Read, FileShare.Read, 8192,
     FileOptions.Asynchronous);
   Byte[] data = new Byte[fs.Length];
   fs.BeginRead(data, 0, data.Length, result => {
     Int32 bytesRead = fs.EndRead(result);
     ProcessData(data);
     fs.Close(); // No 'using'
   }, null);
   // Method returns after issuing async I/O request
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved