コード例 #1
0
ファイル: JobInfo.cs プロジェクト: kmurray-bluevenn/Research
        private void Execute( object threadContext )
        {
            #region TraceBeginElement
            if ( m_Logger.IsTraceEnabled )
            {
                m_Logger.Trace( "Begin ", m_cnt );
            }
            #endregion // TraceBeginElement

            bool isFinished = false;

            progressRetrieval.Invoke( new MethodInvoker( () => progressRetrieval.BackColor = Color.Black ) );
            ProgressPreparation.Invoke( new MethodInvoker( () => ProgressPreparation.BackColor = Color.Black));

            TTransport trans = OpenTransport();

            try
            {
                if ( trans == null )
                {
                    ProgressPreparation.Invoke( new MethodInvoker( () => ProgressPreparation.Value = 100));
                    progressRetrieval.Invoke( new MethodInvoker( () => progressRetrieval.Value = 100));
                    progressRetrieval.Invoke( new MethodInvoker( () => progressRetrieval.BackColor = Color.Red));
                    ProgressPreparation.Invoke( new MethodInvoker( () => ProgressPreparation.BackColor = Color.Red));
                }

                TBinaryProtocol binaryProtocol = new TBinaryProtocol( trans );
                DataProvider.Client client = new DataProvider.Client( binaryProtocol );

                progressRetrieval.Invoke( new MethodInvoker( () => progressRetrieval.BackColor = Color.LightBlue ) );
                ProgressPreparation.Invoke( new MethodInvoker( () => ProgressPreparation.BackColor = Color.LightYellow ) );

                // Prepare some data
                m_JobNumber = client.PrepareDataJob( "Test", new List<string> { "FieldA", "FieldB" }, DateTime.Now.ToTDTO() );

                m_Logger.Trace("job {0} has job id {1}", m_cnt, m_JobNumber);
                if ( lblSubmitted.InvokeRequired )
                {
                    lblSubmitted.Invoke( new MethodInvoker( () => lblSubmitted.Text = DateTime.Now.ToShortTimeString() ) );
                }
                else
                {
                    lblSubmitted.Text = DateTime.Now.ToShortTimeString();
                }

                Application.DoEvents();

                // Monitor progress of prepare job
                ProgressTDTO progress = null;
                while ( !isFinished )
                {
                    progress = client.GetJobProgress( m_JobNumber );

                    ProgressPreparation.Invoke( new MethodInvoker( () => ProgressPreparation.Value = progress.PercentComplete));
                    if ( progress.State == ProgressStateEnumeration.Pending )
                    {
                        ProgressPreparation.Invoke( new MethodInvoker( () => ProgressPreparation.BackColor = Color.LightYellow));
                    }
                    else if ( progress.State == ProgressStateEnumeration.Running )
                    {
                        ProgressPreparation.Invoke( new MethodInvoker( () => ProgressPreparation.BackColor = Color.LightBlue));
                    }
                    else if ( progress.State == ProgressStateEnumeration.Errored )
                    {
                        ProgressPreparation.Invoke( new MethodInvoker( () => ProgressPreparation.BackColor = Color.Red));
                    }
                    else if ( progress.State == ProgressStateEnumeration.Complete )
                    {
                        ProgressPreparation.Invoke( new MethodInvoker( () => ProgressPreparation.ForeColor = Color.LightGreen));
                        isFinished = true;
                    }
                    Application.DoEvents();

                    Thread.Sleep( 300 );
                }

                // Update the row label
                Int64 rowCount = -1;
                if ( progress != null )
                {
                    rowCount = progress.NumberRows;
                }

                if ( lblRowCount.InvokeRequired )
                {
                    lblRowCount.Invoke( new MethodInvoker( () => lblRowCount.Text = rowCount.ToString() ) );
                }
                else
                {
                    lblRowCount.Text = rowCount.ToString();
                }

                // Get the data
                double percentConstant = rowCount / 100.0;
                progressRetrieval.Invoke( new MethodInvoker( () => progressRetrieval.ForeColor = Color.Green ) );

                try
                {
                    for ( Int64 i = 0; i < rowCount; i += m_BatchSize )
                    {
                        List<List<string>> dataRows = client.GetJobData( m_JobNumber, i, m_BatchSize );
                        dataRows.Clear();
                        int percentage = (int) Math.Ceiling( ( i + m_BatchSize ) / percentConstant );
                        percentage = percentage > 100 ? 100 : percentage;
                        progressRetrieval.Invoke( new MethodInvoker( () => progressRetrieval.Value = percentage ) );
                    }
                }
                catch ( Exception )
                {
                    progressRetrieval.Invoke( new MethodInvoker( () => progressRetrieval.ForeColor = Color.Red ) );
                }

            }
            finally
            {
                if ( trans != null )
                {
                    trans.Close();
                }
                #region TraceEndElement
                if ( m_Logger.IsTraceEnabled )
                {
                    m_Logger.Trace( "End-  {0}", m_cnt);
                }
                #endregion // TraceEndElement
            }
        }