protected override void threadmain() { using (updateDB db = new updateDB(Program.connstr, Program.dbName)) { while (true) { if (toMarkComplete.IsEmpty && batches.IsEmpty && batches_wim.IsEmpty) { // we'll only exit the thread if all the current data is flushed. if (exitTime) { if (toMarkComplete.IsEmpty && batches.IsEmpty && batches_wim.IsEmpty) { break; } } pollTimer.WaitOne(TimeSpan.FromSeconds(1)); } { if (toMarkComplete.TryDequeue(out var thisMarkComplete)) { db.completeWsusUpdate(thisMarkComplete.parentID, thisMarkComplete.starttime, thisMarkComplete.endtime, thisMarkComplete.sqltime, thisMarkComplete.succeeded); } } { if (batches.TryDequeue(out fileBatch batch)) { if (batch.Parent.dbID.HasValue == false) { batch.Parent.dbID = db.getWSUSFileByFileHash(batch.Parent.fileHashFromWSUS).dbID; } Debug.WriteLine($"Async inserting {batch.Files.Length} files"); db.bulkInsertFiles(batch.Parent, batch.Files); if (batch.IsFinal) { db.completeWsusUpdate(batch.Parent.dbID.Value, batch.Parent.startTime, batch.Parent.endTime, batch.Parent.sqltime, true); } } } { if (batches_wim.TryDequeue(out fileBatch_wim batchWim)) { if (batchWim.Parent.dbID.HasValue == false) { batchWim.Parent.dbID = db.getWSUSFileByFileHash(batchWim.Parent.fileHashFromWSUS).dbID; } //db.insert_noconcurrency(new[] {batchWim.Files[0].parent}, "fileSource_wim"); db.bulkInsertFiles(batchWim.Parent, batchWim.Files); if (batchWim.IsFinal) { db.completeWsusUpdate(batchWim.Parent.dbID.Value, batchWim.Parent.startTime, batchWim.Parent.endTime, batchWim.Parent.sqltime, true); } } } } } }