public int CloseBatch(int moduleId, TSBatch Batch)
 {
     int retVal = 0;
     if (g_IServer != null)
     {
         int count = 0;
         do
         {
             try
             {
                 string batchData = Batch.ToBlob_Close();
                 retVal = g_IServer.TS2KCloseBatch(g_hTS2KEnv, moduleId, Batch.BatchID, batchData);
                 if (retVal >= 0)
                 {
                     break;
                 }
                 string lastError = GetLastError(retVal);
                 WriteErrorToLog("Could not Close Batch: " + lastError + " Try count: " + count);
             }
             catch (Exception ex)
             {
                 WriteErrorToLog(String.Format("General Error During CloseBatch: {0} - {1}", ex.Message, ex.StackTrace));
             }
             count++;
             Thread.Sleep(delay);
         } while (count < attempt);
     }
     return retVal;
 }
        public int CloseBatch(int ModuleID, TSBatch Batch)
        {
            int retVal = 0;
            try
            {
                retVal = ts2KServerMain.CloseBatch(ModuleID, Batch);

                if (retVal < 0)
                {
                    string msg = ts2KServerMain.GetLastError(retVal);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return retVal;
        }
        public int CreateBatch(int ModuleID, TSBatch Batch, int Flag)
        {
            int BatchID = 0;
            try
            {
                BatchID = ts2KServerMain.CreateBatch(ModuleID, Batch.ToBlob(), Flag);

                if (BatchID <= 0)
                {
                    throw new Exception("General error trying to create batch: " + Batch.BatchName);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return BatchID;
        }
        public List<TSBatch> GetBatchList(int JobID, int ModuleID, int BatchStatus, TSBatch.BatchPriority Priority)
        {
            List<TSBatch> batchList = new List<TSBatch>();
            List<string> ts2kBatchList = new List<string>();

            try
            {
                //Load List of Jobs in string arrJobList
                int retVal = ts2KServerMain.LoadBatchList(ts2kBatchList, JobID, ModuleID, BatchStatus, (int)Priority);
                if (retVal > 0)
                {
                    for (Int32 i = 0; i < ts2kBatchList.Count; i++)
                    {
                        TSBatch batch = new TSBatch();
                        batch.Blob = ts2kBatchList[i];
                        batch.BatchID = mem.GetInt("Batches", "BatchID", 0, batch.Blob);
                        batch.BatchName = mem.GetString("Batches", "BatchName", String.Empty, batch.Blob);
                        batch.BatchBox = mem.GetString("Batches", "BatchBox", String.Empty, batch.Blob);
                        batch.BatchPath = mem.GetString("Batches", "BatchPath", String.Empty, batch.Blob);
                        batch.BatchDesc = mem.GetString("Batches", "BatchDesc", String.Empty, batch.Blob);
                        batch.JobID = mem.GetInt("Batches", "JobID", 0, batch.Blob);
                        batch.BatchLocation = mem.GetInt("Batches", "BatchLocation", 0, batch.Blob);
                        batch.BatchStatus = mem.GetInt("Batches", "BatchStatus", 0, batch.Blob);
                        batch.Priority = (TSBatch.BatchPriority)mem.GetInt("Batches", "Priority", 1, batch.Blob);
                        batch.AppStep = mem.GetInt("Batches", "AppStep", 0, batch.Blob);
                        batch.WFStep = mem.GetInt("Batches", "WFStep", 0, batch.Blob);
                        batch.CreatedBy = mem.GetInt("Batches", "CreatedBy", 0, batch.Blob);
                        batch.TotalImages = mem.GetInt("Batches", "TotalImages", 0, batch.Blob);
                        batch.DeletedImages = mem.GetInt("Batches", "DeletedImages", 0, batch.Blob);
                        batch.TimeStamp = mem.GetInt("Batches", "TimeStamp", 0, batch.Blob);
                        batch.Comments = mem.GetString("Batches", "Comments", String.Empty, batch.Blob);

                        if (!String.IsNullOrWhiteSpace(batch.Blob))
                        {
                            batchList.Add(batch);
                        }
                    }
                }
                else if (retVal < 0)
                {
                    string sMsg = ts2KServerMain.GetLastError(retVal);
                    throw new Exception(String.Format("Failed to get batch list: {0}", sMsg));
                }
                else
                {
                    throw new Exception("Could not find any batches!");
                }
            }
            catch (Exception)
            {
                throw;
            }
            return batchList;
        }
        private void btnCreateBatch_Click(object sender, EventArgs e)
        {
            try
            {
                var sw = new Stopwatch();
                sw.Start();

                var batchName = tbxBatchName.Text.Trim();
                if (string.IsNullOrWhiteSpace(batchName) || batchName.Length > 127)
                {
                    throw new ArgumentException("BatchName cannot be empty and cannot exceed 127 characters!");
                }
                var batchDescription = tbxBatchDescription.Text;
                var batchComments = tbxBatchComments.Text;
                var timeStamp = Support.Date2Unix(DateTime.Now);

                var newBatch = new TSBatch(batchName, Job.JobID, TsServer.CurrentUser.UserID,
                    TSBatch.BatchPriority.Normal)
                {
                    BatchDesc = batchDescription,
                    Comments = batchComments,
                    TimeStamp = (int) timeStamp
                };

                //  Get Batch Fields
                foreach (Control tb in tlpBatchFields.Controls)
                {
                    if (tb.Tag == null) continue;
                    var field = (TSIndexField)tb.Tag;
                    field.Value = tb.Text;

                    newBatch.BatchFields.Add(field);
                }

                var batchId = 0;
                if (ImageFileList != null && ImageFileList.Count > 0)
                {
                    batchId = TsServer.CreateBatch(TS2KConst.TS2K_MODULE_SCAN, newBatch, TS2KConst.TS2K_BATCH_OPENNEW_FIXPATH);
                    newBatch.BatchID = batchId;

                    if (batchId > 0)
                    {
                        var batchPath = Path.Combine(Job.DefaultPath, "Batch" + batchId);
                        var imageList = new List<TSImage>();
                        var autoFail = 0;
                        var moveQueue = 1;
                        var imgCounter = 0;

                        //  Write Images to Server
                        foreach (var filePath in ImageFileList)
                        {
                            var retVal = TsServer.FileSave(filePath, Path.Combine(batchPath, Path.GetFileName(filePath)));
                            if (retVal)
                            {
                                imgCounter++;

                                var image = new TSImage
                                {
                                    ImageName = Path.GetFileName(filePath),
                                    ImagePath = batchPath,
                                    ProcessedBy = TsServer.CurrentUser.UserID
                                };
                                image.Levels[0] = imgCounter;

                                imageList.Add(image);
                            }
                            else
                            {
                                autoFail = 1;
                                moveQueue = 0;
                            }
                        }

                        //  Write Image Results
                        if (TsServer.WriteImageResults(imageList, batchId, (int)TS2KConst.TS2K_MODULE_SCAN) <= 0)
                        {
                            autoFail = 1;
                            moveQueue = 0;
                        }

                        sw.Stop();
                        var appProcTime = (int)sw.ElapsedMilliseconds / 1000;

                        //  Close the Batch
                        newBatch.TotalImages = imageList.Count;
                        newBatch.AutoFail = autoFail;
                        newBatch.MoveQueue = moveQueue;
                        if (moveQueue > 0)
                        {
                            newBatch.BatchLocation = 2;
                            //newBatch.MoveFlag = 3;
                        }
                        newBatch.AppProcTime = appProcTime;

                        if (TsServer.SetBatch(TS2KConst.TS2K_MODULE_ENHANCE, batchId, newBatch.ToBlob_Set()) < 0)
                        {
                            MessageBox.Show(string.Format("Batch could not be set in Enhance!"), System.Reflection.Assembly.GetEntryAssembly().GetName().Name, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }

                        if (TsServer.CloseBatch(TS2KConst.TS2K_MODULE_ENHANCE, newBatch) < 0)
                        {
                            throw new Exception("Batch could not be closed!");
                        }

                    }
                }
                else
                {
                    batchId = TsServer.CreateBatch(TS2KConst.TS2K_MODULE_SCAN, newBatch, TS2KConst.TS2K_BATCH_CREATENEW_FIXPATH);
                }

                MessageBox.Show(string.Format("Batch {0} created successfully!", newBatch.BatchName), System.Reflection.Assembly.GetEntryAssembly().GetName().Name, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Error with creating batch: {0}", ex.Message), System.Reflection.Assembly.GetEntryAssembly().GetName().Name, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }