public void ThreadPoolGUIDAssignCallback(Object threadContext) { GUIDState stateInfo = (GUIDState)threadContext; ManualResetEvent manualEvent = stateInfo.manualEvent; int threadIndex = stateInfo.threadIndex; int min = stateInfo.min; int max = stateInfo.max; Debug.Print("Thread started for GUID assign..."); AssignGUIDs(min, max); Debug.Print("Thread completed for GUID assign..."); manualEvent.Set(); }
/// <summary> /// Start the conversion process /// </summary> public void Convert() { #region Debug stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); System.Diagnostics.Debug.Print(DateTime.Now + " : " + "Table-to-form GUID creation initiated. Elapsed: " + stopwatch.Elapsed.TotalSeconds.ToString()); #endregion // Debug int artifacts = 0; if (SetStatus != null) { SetStatus("Getting data..."); } DT = sourceDriver.GetTableData(tableName, columnNames); artifacts = (DT.Rows.Count * destinationView.Pages.Count) + DT.Rows.Count; if (SetMaxProgressBarValue != null) { SetMaxProgressBarValue((double)artifacts); } #region Set up global record ID values if (SetStatus != null) { SetStatus("Starting global record ID generation..."); } DT.Columns.Add(new DataColumn("GlobalRecordId", typeof(string))); int currentRow = 0; foreach (DataRow row in DT.Rows) { string GUID = System.Guid.NewGuid().ToString(); row["GlobalRecordId"] = GUID; currentRow++; } System.Diagnostics.Debug.Print(DateTime.Now + " : " + "All GUIDs generated. Starting GUID assignment. Elapsed: " + stopwatch.Elapsed.TotalSeconds.ToString()); // set PK DataColumn[] parentPrimaryKeyColumns = new DataColumn[1]; parentPrimaryKeyColumns[0] = DT.Columns["GlobalRecordId"]; DT.PrimaryKey = parentPrimaryKeyColumns; int min = 0; int max = DT.Rows.Count; int q = max / 4; int Q1 = min + q; int Q2 = Q1 + q; int Q3 = max - q; conn = destinationDriver.GetConnection(); conn.Open(); doneEvents = new ManualResetEvent[4]; Debug.Print("Launching 4 GUID assignment tasks..."); GUIDState guidStateInfo; doneEvents[0] = new ManualResetEvent(false); guidStateInfo = new GUIDState(doneEvents[0], 0, 0, Q1); ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPoolGUIDAssignCallback), guidStateInfo); doneEvents[1] = new ManualResetEvent(false); guidStateInfo = new GUIDState(doneEvents[1], 1, Q1, Q2); ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPoolGUIDAssignCallback), guidStateInfo); doneEvents[2] = new ManualResetEvent(false); guidStateInfo = new GUIDState(doneEvents[2], 2, Q2, Q3); ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPoolGUIDAssignCallback), guidStateInfo); doneEvents[3] = new ManualResetEvent(false); guidStateInfo = new GUIDState(doneEvents[3], 3, Q3, max); ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPoolGUIDAssignCallback), guidStateInfo); WaitHandle.WaitAll(doneEvents); #endregion // Set up global record ID values doneEvents = null; if (SetStatus != null) { SetStatus("Populating field data..."); } System.Diagnostics.Debug.Print(DateTime.Now + " : " + "All GUIDs assigned. Starting page data import. Elapsed: " + stopwatch.Elapsed.TotalSeconds.ToString()); int pageCount = destinationView.Pages.Count; doneEvents = new ManualResetEvent[pageCount]; Debug.Print("Launch {0} tasks...", pageCount); ImportState stateInfo; for (int i = 0; i < pageCount; i++) { doneEvents[i] = new ManualResetEvent(false); stateInfo = new ImportState(destinationView.Pages[i], doneEvents[i], i); ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPoolPageImportCallback), stateInfo); } WaitHandle.WaitAll(doneEvents); conn.Close(); conn.Dispose(); conn = null; stopwatch.Stop(); System.Diagnostics.Debug.Print(DateTime.Now + " : " + "Processing complete. Elapsed: " + stopwatch.Elapsed.TotalSeconds.ToString()); }