public void MergeRunners(Runner[] runners)
        {
            if (runners == null)
                return;

            foreach (var r in runners)
            {
                if (!IsRunnerAdded(r.ID))
                {
                    AddRunner(new Runner(r.ID, r.Name, r.Club, r.Class, r.SourceId));
                }
                else
                {
                    UpdateRunnerInfo(r.ID, r.Name, r.Club, r.Class, r.SourceId);
                }

                if (r.StartTime >= 0)
                    SetRunnerStartTime(r.ID, r.StartTime);

                SetRunnerResult(r.ID, r.Time, r.Status);

                var spl = r.SplitTimes;
                if (spl != null)
                {
                    foreach (var s in spl)
                    {
                        SetRunnerSplit(r.ID, s.Control, s.Time);
                    }
                }
            }
        }
        public void Start()
        {
            FireLogMsg("Buffering existing results..");
            int numRunners = 0;
            int numResults = 0;
            try
            {
                m_currentlyBuffering = true;
                m_connection.Open();

                SetCodePage(m_connection);

                MySqlCommand cmd = m_connection.CreateCommand();

                if (!m_compsSourceToIdMapping.ContainsKey(m_compID))
                {
                    m_compsSourceToIdMapping.Add(m_compID, new Dictionary<string, int>());
                    m_compsNextGeneratedId.Add(m_compID, -1);
                }

                cmd.CommandText = "select sourceid,id from runneraliases where compid = " + m_compID;
                MySqlDataReader reader = cmd.ExecuteReader();

                Dictionary<int,string> idToAliasDictionary = new Dictionary<int, string>();

                while (reader.Read())
                {
                    var sourceId = reader["sourceid"] as string;
                    if (sourceId == null)
                        continue;
                    int id = Convert.ToInt32(reader["id"]);
                    if (!m_compsSourceToIdMapping[m_compID].ContainsKey(sourceId))
                    {
                        m_compsSourceToIdMapping[m_compID].Add(sourceId, id);
                        if (id <= m_compsNextGeneratedId[m_compID])
                            m_compsNextGeneratedId[m_compID] = id - 1;
                    }
                }
                reader.Close();

                foreach (var kvp in m_compsSourceToIdMapping[m_compID])
                {
                    if (!idToAliasDictionary.ContainsKey(kvp.Value))
                        idToAliasDictionary.Add(kvp.Value, kvp.Key);
                }

                cmd.CommandText = "select runners.dbid,control,time,name,club,class,status from runners, results where results.dbid = runners.dbid and results.tavid = " + m_compID + " and runners.tavid = " + m_compID;
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    var dbid = Convert.ToInt32(reader["dbid"]);
                    var control = Convert.ToInt32(reader["control"]);
                    var time = Convert.ToInt32(reader["time"]);
                    var sourceId = idToAliasDictionary.ContainsKey(dbid) ? idToAliasDictionary[dbid] : null;
                    if (!IsRunnerAdded(dbid))
                    {
                        var r = new Runner(dbid, reader["name"] as string, reader["club"] as string, reader["class"] as string, sourceId);
                        AddRunner(r);
                        numRunners++;
                    }
                switch (control)
                    {
                        case 1000:
                            SetRunnerResult(dbid, time, Convert.ToInt32(reader["status"]));
                            numResults++;
                            break;
                        case 100:
                            SetRunnerStartTime(dbid, time);
                            numResults++;
                            break;
                        default:
                            numResults++;
                            SetRunnerSplit(dbid, control, time);
                            break;
                    }

                }
                reader.Close();

                cmd.Dispose();

                ResetUpdated();
            }
            catch (Exception ee)
            {
                FireLogMsg(ee.Message);
                Thread.Sleep(1000);
            }
            finally
            {
                m_connection.Close();
                m_itemsToUpdate.Clear();
                m_currentlyBuffering = false;
                FireLogMsg("Done - Buffered " + m_runners.Count + " existing runners and " + numResults +" existing results from server");
            }

            m_continue = true;
            m_mainTh = new Thread(Run);
            m_mainTh.Name = "Main MYSQL Thread [" + m_connection.DataSource + "]";
            m_mainTh.Start();
        }
 /// <summary>
 /// Adds a Runner to this competition
 /// </summary>
 /// <param name="r"></param>
 public void AddRunner(Runner r)
 {
     if (!m_runners.ContainsKey(r.ID))
     {
         m_runners.Add(r.ID, r);
         m_itemsToUpdate.Add(r);
         if (!m_currentlyBuffering)
         {
             FireLogMsg("Runner added [" + r.Name + "]");
         }
     }
 }