crashanalysercmd/Libraries/Engine/CrashDebuggerLib/Threading/AsyncOperationManager.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2004-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 using System;
       
    19 using System.Collections.Generic;
       
    20 using System.Text;
       
    21 using System.Threading;
       
    22 using System.ComponentModel;
       
    23 
       
    24 namespace CrashDebuggerLib.Threading
       
    25 {
       
    26     // <summary>
       
    27     // Serialises asynchronous requests - mainly because the symbol engine doesn't much
       
    28     // like having dynamically loaded codesegments unloaded underneath it - i.e. it doesn't
       
    29     // work multithreaded!
       
    30     // </summary>
       
    31     internal class AsyncOperationManager
       
    32     {
       
    33         #region Constructors
       
    34         public AsyncOperationManager()
       
    35         {
       
    36         }
       
    37         #endregion
       
    38 
       
    39         #region API
       
    40         public void Clear()
       
    41         {
       
    42             lock ( this )
       
    43             {
       
    44                 iStarted = false;
       
    45                 iQueue.Clear();
       
    46             }
       
    47         }
       
    48 
       
    49         public void Start()
       
    50         {
       
    51             lock ( this )
       
    52             {
       
    53                 iStarted = true;
       
    54                 StartNextOperation();
       
    55             }
       
    56         }
       
    57 
       
    58         public void Queue( AsyncOperation aOperation )
       
    59         {
       
    60             Queue( aOperation, false );
       
    61         }
       
    62 
       
    63         public void Queue( AsyncOperation aOperation, bool aHighPriority )
       
    64         {
       
    65             lock ( this )
       
    66             {
       
    67                 if ( aHighPriority )
       
    68                 {
       
    69                     iQueue.Insert( 0, aOperation );
       
    70                 }
       
    71                 else
       
    72                 {
       
    73                     iQueue.Add( aOperation );
       
    74                 }
       
    75             }
       
    76 
       
    77             //System.Diagnostics.Debug.WriteLine( "[AOP] - Add - Queue now contains " + iQueue.Count + " entries..." );
       
    78             StartNextOperation();
       
    79         }
       
    80         #endregion
       
    81 
       
    82         #region Event handlers
       
    83         void Operation_RunWorkerCompleted( object aSender, RunWorkerCompletedEventArgs aArgs )
       
    84         {
       
    85             lock ( this )
       
    86             {
       
    87                 AsyncOperation op = (AsyncOperation) aSender;
       
    88                 op.RunWorkerCompleted -= new RunWorkerCompletedEventHandler( Operation_RunWorkerCompleted );
       
    89                 iPendingOperation = false;
       
    90             }
       
    91             //
       
    92             StartNextOperation();
       
    93             //System.Diagnostics.Debug.WriteLine( "[AOP] - Fin -Queue now contains " + iQueue.Count + " entries..." );
       
    94         }
       
    95         #endregion
       
    96 
       
    97         #region Properties
       
    98         #endregion
       
    99 
       
   100         #region Internal methods
       
   101         private void StartNextOperation()
       
   102         {
       
   103             lock ( this )
       
   104             {
       
   105                 if ( iStarted )
       
   106                 {
       
   107                     if ( iQueue.Count > 0 && !iPendingOperation )
       
   108                     {
       
   109                         AsyncOperation op = iQueue[ 0 ];
       
   110                         iQueue.RemoveAt( 0 );
       
   111                         //
       
   112                         op.RunWorkerCompleted += new RunWorkerCompletedEventHandler( Operation_RunWorkerCompleted );
       
   113                         iPendingOperation = true;
       
   114                         op.RunWorkerAsync( op );
       
   115                         //
       
   116                         //System.Diagnostics.Debug.WriteLine( "[AOP] - Start - Starting op with " + iQueue.Count + " remaining..." );
       
   117                     }
       
   118                     else
       
   119                     {
       
   120                         System.Diagnostics.Debug.WriteLine( "[AOP] - Start - Is empty!" );
       
   121                     }
       
   122                 }
       
   123                 else
       
   124                 {
       
   125                     System.Diagnostics.Debug.WriteLine( "[AOP] - Start - Queue is disabled!" );
       
   126                 }
       
   127             }
       
   128         }
       
   129         #endregion
       
   130 
       
   131         #region Data members
       
   132         private List<AsyncOperation> iQueue = new List<AsyncOperation>();
       
   133         private bool iPendingOperation = false;
       
   134         private bool iStarted = false;
       
   135         #endregion
       
   136     }
       
   137 }