crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianDebugLib/Entity/Primer/DbgEntityPrimerUi.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2009 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 using System;
       
    18 using System.IO;
       
    19 using System.Collections.Generic;
       
    20 using System.Text;
       
    21 using System.Threading;
       
    22 using System.Drawing;
       
    23 using System.Collections;
       
    24 using System.ComponentModel;
       
    25 using System.Windows.Forms;
       
    26 using SymbianUtils;
       
    27 using SymbianUtils.PluginManager;
       
    28 using SymbianUtils.FileSystem;
       
    29 using SymbianUtils.Settings;
       
    30 using SymbianDebugLib.Engine;
       
    31 using SymbianDebugLib.PluginAPI;
       
    32 
       
    33 namespace SymbianDebugLib.Entity.Primer
       
    34 {
       
    35     public partial class DbgEntityPrimerUi : Form, IDbgEntityPrimer
       
    36     {
       
    37         #region Constructors
       
    38         public DbgEntityPrimerUi( DbgEntity aEntity, DbgPluginEngine aPlugin )
       
    39         {
       
    40             iEntity = aEntity;
       
    41             iPlugin = aPlugin;
       
    42 
       
    43             // Make a new primer and seed it with the entity.
       
    44             iPrimer = aPlugin.CreatePrimer();
       
    45             iPrimer.Add( aEntity );
       
    46 
       
    47             // Listen to plugin primer events
       
    48             iPrimer.EventHandler += new DbgPluginPrimer.PrimeEventHandler( PrimerPlugin_EventHandler );
       
    49             //
       
    50             this.InitializeComponent();
       
    51             this.Text = string.Format( "Preparing: [{0}]", Path.GetFileName( aEntity.FullName ) );
       
    52         }
       
    53         #endregion
       
    54 
       
    55         #region From IDbgEntityPrimer
       
    56         public void Prime( TSynchronicity aSynchronicity )
       
    57         {
       
    58             // Timer initiates operation
       
    59             iTimer_OpStart.Tag = aSynchronicity;
       
    60             iTimer_OpStart.Start();
       
    61 
       
    62             // Show the dialog
       
    63             base.ShowDialog();
       
    64         }
       
    65 
       
    66         public string PrimeErrorMessage
       
    67         {
       
    68             get { return iEntity.PrimerResult.PrimeErrorMessage; }
       
    69         }
       
    70 
       
    71         public Exception PrimeException
       
    72         {
       
    73             get { return iEntity.PrimerResult.PrimeException; }
       
    74             internal set { iEntity.PrimerResult.PrimeException = value; }
       
    75         }
       
    76         #endregion
       
    77 
       
    78         #region Event handlers
       
    79         private void Timer_OpStart_Tick( object sender, EventArgs aArgs )
       
    80         {
       
    81             iTimer_OpStart.Stop();
       
    82             iTimer_OpStart.Enabled = false;
       
    83             //
       
    84             TSynchronicity syncMode = (TSynchronicity) iTimer_OpStart.Tag;
       
    85             PrimeException = null;
       
    86 
       
    87             // If requested to prime asynchronously then we don't need to 
       
    88             // do anything because this will return immediately and the
       
    89             // prime will run in a background thread.
       
    90             //
       
    91             // On the other hand, if requesting synchronous priming then 
       
    92             // we need to spawn a worker thread or else the progress dialog
       
    93             // will not redraw (since the synchronous operation runs within
       
    94             // the context of the thread in which the synchronous prime request
       
    95             // originates).
       
    96             switch ( syncMode )
       
    97             {
       
    98             case TSynchronicity.EAsynchronous:
       
    99                 RunPrime( TSynchronicity.EAsynchronous );
       
   100                 break;
       
   101             case TSynchronicity.ESynchronous:
       
   102                 ThreadPool.QueueUserWorkItem( new WaitCallback( RunSyncPrimeInWorkerThread ) );
       
   103                 break;
       
   104             default:
       
   105                 throw new NotSupportedException( "Unsupported synchronicity" );
       
   106             }
       
   107         }
       
   108         #endregion
       
   109 
       
   110         #region Event handlers
       
   111         private void PrimerPlugin_EventHandler( DbgPluginPrimer.TPrimeEvent aEvent, object aData )
       
   112         {
       
   113             System.Diagnostics.Debug.WriteLine( string.Format( "PrimerPlugin - event handler - {0} - {1}", aEvent, aData ) );
       
   114             if ( InvokeRequired )
       
   115             {
       
   116                 DbgPluginPrimer.PrimeEventHandler callback = new DbgPluginPrimer.PrimeEventHandler( PrimerPlugin_EventHandler );
       
   117                 this.BeginInvoke( callback, new object[] { aEvent, aData } );
       
   118             }
       
   119             else
       
   120             {
       
   121                 switch ( aEvent )
       
   122                 {
       
   123                 case DbgPluginPrimer.TPrimeEvent.EEventPrimingStarted:
       
   124                     iProgressBar.Maximum = 100; //%
       
   125                     iProgressBar.Minimum = 0; //%
       
   126                     iProgressBar.Value = 0;
       
   127                     OnPrimeStart();
       
   128                     break;
       
   129                 case DbgPluginPrimer.TPrimeEvent.EEventPrimingProgress:
       
   130                     if ( aData != null && ( aData is int ) )
       
   131                     {
       
   132                         int prog = (int) aData;
       
   133                         OnPrimeProgress( prog );
       
   134                         iProgressBar.Value = prog;
       
   135                     }
       
   136                     break;
       
   137                 case DbgPluginPrimer.TPrimeEvent.EEventPrimingComplete:
       
   138                     iProgressBar.Value = 100;
       
   139                     OnPrimeComplete();
       
   140                     Close();
       
   141                     break;
       
   142                 }
       
   143             }
       
   144             //
       
   145             Application.DoEvents();
       
   146         }
       
   147         #endregion
       
   148 
       
   149         #region Event cascading
       
   150         protected void OnPrimeStart()
       
   151         {
       
   152             bool sent = ( ( iFlags & TFlags.EFlagsSentEventStart ) == TFlags.EFlagsSentEventStart );
       
   153             if ( !sent )
       
   154             {
       
   155                 iEntity.OnPrimeStart( this );
       
   156                 iFlags |= TFlags.EFlagsSentEventStart;
       
   157             }
       
   158         }
       
   159 
       
   160         protected void OnPrimeProgress( int aValue )
       
   161         {
       
   162             iEntity.OnPrimeProgress( this, aValue );
       
   163         }
       
   164 
       
   165         protected void OnPrimeComplete()
       
   166         {
       
   167             bool sent = ( ( iFlags & TFlags.EFlagsSentEventComplete ) == TFlags.EFlagsSentEventComplete );
       
   168             if ( !sent )
       
   169             {
       
   170                 // Update entity's primer results
       
   171                 iEntity.OnPrimeComplete( this );
       
   172                 iFlags |= TFlags.EFlagsSentEventComplete;
       
   173             }
       
   174         }
       
   175         #endregion
       
   176 
       
   177         #region Internal enumerations
       
   178         [Flags]
       
   179         private enum TFlags
       
   180         {
       
   181             EFlagsNone = 0,
       
   182             EFlagsSentEventStart = 1,
       
   183             EFlagsSentEventComplete = 2
       
   184         }
       
   185         #endregion
       
   186 
       
   187         #region Internal methods
       
   188         private void RunSyncPrimeInWorkerThread( object aNotUsed )
       
   189         {
       
   190             RunPrime( TSynchronicity.ESynchronous );
       
   191         }
       
   192 
       
   193         private void RunPrime( TSynchronicity aSynchronicity )
       
   194         {
       
   195             try
       
   196             {
       
   197                 iPrimer.Prime( aSynchronicity );
       
   198             }
       
   199             catch ( Exception e )
       
   200             {
       
   201                 OnPrimeStart();
       
   202                 PrimeException = e;
       
   203                 OnPrimeComplete();
       
   204             }
       
   205         }
       
   206         #endregion
       
   207 
       
   208         #region Data members
       
   209         private readonly DbgEntity iEntity;
       
   210         private readonly DbgPluginEngine iPlugin;
       
   211         private readonly DbgPluginPrimer iPrimer;
       
   212         private TFlags iFlags = TFlags.EFlagsNone;
       
   213         #endregion
       
   214     }
       
   215 }