public MessageHandler(SynchronizationContext windowsFormsContext, ReplicationTaskInfo source, TaskEvent onStatusUI, TaskEvent onErrorUI)
 {
     this.OnStatusUI          = onStatusUI;
     this.OnErrorUI           = onErrorUI;
     this.windowsFormsContext = windowsFormsContext;
     this.source = source;
 }
        public void Replicate(ReplicationTaskInfo job)
        {
            try
            {
                job.IsRunning = true;

                DbConnectionInfo s = new DbConnectionInfo(job.ConnectionStringSource, job.DialectSource);
                DbConnectionInfo d = new DbConnectionInfo(job.ConnectionStringDestination, job.DialectDestination);

                /*
                 * d.status += new DlgStatusMsg(r_status);
                 * d.statuspgb += new DlgStatusPgb(r_statuspgb);
                 * d.everros += new DlgStatusMsg(r_erros);
                 */

                string status = "Table {0} of {1} - {2}";

                int totalTables = job.TablesAvailable.Count(w => w.Checked);
                int curTable    = 1;

                using (DbCon dbSource = s.CreateConnection())
                    using (DbCon dbDest = d.CreateConnection())
                    {
                        foreach (TableInfo table in job.TablesAvailable.Where(w => w.Checked))
                        {
                            bool workDoneForThisTable = table.CompareEntireTableAtOnce;

                            long   curMinId      = 0;
                            long   curMaxId      = 0;
                            string columnKeyName = null;
                            long   maxId         = 0;

                            if (!table.CompareEntireTableAtOnce)
                            {
                                columnKeyName = table.ColumnKeyName;
                                if (!string.IsNullOrWhiteSpace(columnKeyName))
                                {
                                    maxId = dbSource.GetMax(table.TableName, columnKeyName);
                                }
                            }


                            do
                            {
                                curMinId = curMaxId;
                                curMaxId = curMinId + table.IdRangeSize;

                                messageHandler.SendStatus(string.Format(status, curTable, totalTables, "Loading " + table + " [" + curMinId + "-" + curMaxId + "]"));

                                //Load Source Table (Async)
                                DbTableLoader dblo1 = new DbTableLoader(dbSource, messageHandler);

                                Task <Table> taskTb1 = Task.Run(() =>
                                {
                                    return(dblo1.LoadTableData(table, null, columnKeyName, curMinId, curMaxId, job.RetrieveDataCondition));
                                });

                                //Load Destination Table (Sync)
                                DbTableLoader dblo2 = new DbTableLoader(dbDest, messageHandler);

                                Table tb2 = dblo2.LoadTableData(table, table, columnKeyName, curMinId, curMaxId, job.RetrieveDataCondition);

                                //Wait finish loading Source Table
                                taskTb1.Wait();
                                Table tb1 = taskTb1.Result;


                                if (tb1.Data.Count == 0)
                                {
                                    workDoneForThisTable = curMaxId > maxId;
                                }

                                messageHandler.SendStatus(string.Format(status, curTable, totalTables, "Syncing " + table));

                                //Execute Synchronization
                                DbSyncRunner dbSyncRunner = new DbSyncRunner(dbDest, messageHandler);
                                dbSyncRunner.OnProgress = delegate(double v, double max)
                                {
                                    job.OnProgress(CalcProgress(curMinId, curMaxId, maxId, v, max), 100);
                                };

                                dbSyncRunner.ExecuteInstructions(tb1, tb2);


                                job.OnProgress(CalcProgress(curMinId, curMaxId, maxId, 100, 100), 100);

                                if (Replicator.AbortReplication)
                                {
                                    throw new ApplicationException("Aborted");
                                }
                            } while (!workDoneForThisTable);

                            curTable++;
                        }
                    }

                messageHandler.SendStatus("Finished");
            }
            catch (Exception e)
            {
                messageHandler.SendError(e.Message);
                messageHandler.SendStatus("Error " + e.Message);
            }
            finally
            {
                job.LastTimeReplicated = DateTime.Now.Ticks;
                job.IsRunning          = false;
            }
        }