private static void CacheTripInstances(ref SQLiteDataReader reader,
                                               ref int processedRowCount, Stopwatch timeSoFar,
                                               ref string current_table_name, Action displayTableName, Action <int, Stopwatch> updateTimeUI,
                                               ref Dictionary <long, List <trip_instance> > eids,
                                               ref Dictionary <long, long> linefeatures)
        {
            // Update the table name shown on the progress form
            if (displayTableName != null)
            {
                displayTableName();
            }

            // Loop through the trip instances and add a list of them to a dictionary keyed by EID.
            try
            {
                while (reader.Read())
                {
                    ++processedRowCount;
                    if (updateTimeUI != null)
                    {
                        updateTimeUI(processedRowCount, timeSoFar);
                    }

                    trip_instance TI = new trip_instance();
                    TI.trip_id    = reader["trip_id"].ToString();
                    TI.start_time = Convert.ToInt32(reader["start_time"].ToString());
                    TI.end_time   = Convert.ToInt32(reader["end_time"].ToString());

                    long SourceOID = Convert.ToInt64(reader["SourceOID"].ToString());
                    long EID;
                    try{ EID = linefeatures[SourceOID]; }
                    catch
                    {
                        // If there's a problem, it's probably because there was a build error and
                        // the sourceOID never got put into the network, so an EID was never generated
                        // Just skip these.
                        continue;
                    }

                    // Add the trip to the dictionary under the appropriate eid.
                    if (eids.ContainsKey(EID))
                    {
                        eids[EID].Add(TI);
                    }
                    else
                    {
                        eids.Add(EID, new List <trip_instance>()
                        {
                            TI
                        });
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception("Error caching trip instances table. Error: " + e.Message, e);
            }
        }
        private static void CalculateTravelTime(esriNetworkTimeUsage timeUsage, double secondsSinceMidnight, trip_instance instance, ref double final_travel_time)
        {
            // Going forward in time
            if (timeUsage == esriNetworkTimeUsage.esriNTUBeforeTraversal)
            {
                // Select only those trips with a start time after the query time.
                // Use a half-second for padding to avoid small rounding errors that come out of core Network Analyst
                if (instance.start_time >= secondsSinceMidnight-0.5)
                {
                    double travel_time = instance.end_time - secondsSinceMidnight;
                    if (travel_time < final_travel_time)
                    {
                        // If the travel time we just calculated is less than our current minimum,
                        // update the current minimum.
                        final_travel_time = travel_time;
                    }
                }
            }

            // Going backward in time
            else //esriNetworkTimeUsage.esriNTUAfterTraversal
            {
                // Select only those trips with an end time before the query time.
                if (instance.end_time <= secondsSinceMidnight+0.5)
                {
                    // How long between the query time and the time you ended your trip at that stop.
                    double travel_time = secondsSinceMidnight - instance.start_time;
                    if (travel_time < final_travel_time)
                    {
                        // If the travel time we just calculated is less than our current minimum,
                        // update the current minimum.
                        final_travel_time = travel_time;
                    }
                }
            }
        }
        private static void CacheTripInstances(ref SQLiteDataReader reader,
            ref int processedRowCount, Stopwatch timeSoFar,
            ref string current_table_name, Action displayTableName, Action<int, Stopwatch> updateTimeUI,
            ref Dictionary<long, List<trip_instance>> eids,
            ref Dictionary<long, long> linefeatures)
        {
            // Update the table name shown on the progress form
            if (displayTableName != null)
                displayTableName();

            // Loop through the trip instances and add a list of them to a dictionary keyed by EID.
            try
            {
                while (reader.Read())
                {
                    ++processedRowCount;
                    if (updateTimeUI != null)
                        updateTimeUI(processedRowCount, timeSoFar);

                    trip_instance TI = new trip_instance();
                    TI.trip_id = reader["trip_id"].ToString();
                    TI.start_time = Convert.ToInt32(reader["start_time"].ToString());
                    TI.end_time = Convert.ToInt32(reader["end_time"].ToString());

                    long SourceOID = Convert.ToInt64(reader["SourceOID"].ToString());
                    long EID;
                    try{EID = linefeatures[SourceOID];}
                    catch
                    {
                        // If there's a problem, it's probably because there was a build error and
                        // the sourceOID never got put into the network, so an EID was never generated
                        // Just skip these.
                        continue;
                    }

                    // Add the trip to the dictionary under the appropriate eid.
                    if (eids.ContainsKey(EID))
                    {
                        eids[EID].Add(TI);
                    }
                    else
                    {
                        eids.Add(EID, new List<trip_instance>() { TI });
                    }
                }
                   
            }
            catch (Exception e)
            {
                throw new Exception("Error caching trip instances table. Error: " + e.Message, e);
            }
        }
        private static void CalculateTravelTime(esriNetworkTimeUsage timeUsage, double secondsSinceMidnight, trip_instance instance, ref double final_travel_time)
        {
            // Going forward in time
            if (timeUsage == esriNetworkTimeUsage.esriNTUBeforeTraversal)
            {
                // Select only those trips with a start time after the query time.
                // Use a half-second for padding to avoid small rounding errors that come out of core Network Analyst
                if (instance.start_time >= secondsSinceMidnight - 0.5)
                {
                    double travel_time = instance.end_time - secondsSinceMidnight;
                    if (travel_time < final_travel_time)
                    {
                        // If the travel time we just calculated is less than our current minimum,
                        // update the current minimum.
                        final_travel_time = travel_time;
                    }
                }
            }

            // Going backward in time
            else //esriNetworkTimeUsage.esriNTUAfterTraversal
            {
                // Select only those trips with an end time before the query time.
                if (instance.end_time <= secondsSinceMidnight + 0.5)
                {
                    // How long between the query time and the time you ended your trip at that stop.
                    double travel_time = secondsSinceMidnight - instance.start_time;
                    if (travel_time < final_travel_time)
                    {
                        // If the travel time we just calculated is less than our current minimum,
                        // update the current minimum.
                        final_travel_time = travel_time;
                    }
                }
            }
        }