Is this the correct way to lock and modify a DataTable that is used by multiple threads? If not, what would be the right way to do this?
private void DoGeocodeLookup(object info) { ParameterRow data = (ParameterRow)info; DataRow dr = data.Dr; int localIndex = data.Index; ManualResetEvent doneEvent = data.m_doneEvent; Geocode_Google.GeoCode gc = new GeoCode(); gc.Addr_In = m_dtAddress.Rows[localIndex]["AddressStd_tx"].ToString(); gc.GeoCodeOne(); if (gc.Status == "OK") { //write back to temporary datatable lock( m_TempTable.Rows.SyncRoot ) { m_TempTable.Rows[localIndex]["GL_Address"] = gc.Thoroughfare_tx; } } doneEvent.Set(); }
My structure:
struct ParameterRow { private DataRow m_row; private int m_index; public DataRow Dr { get { return m_row; } set { m_row = value; } } public int Index { get { return m_index; } set { m_index = value; } } public ManualResetEvent m_doneEvent; public ParameterRow( DataRow row, int index, ManualResetEvent doneEvent) { m_row = row; m_index = index; m_doneEvent = doneEvent; } }
Snippet where I start all threads:
//make temporary table m_TempTable = new DataTable(); m_TempTable = m_dtAddress.Copy(); for (int index = 0; index < m_geocodes; index++) { doneEvents[index] = new ManualResetEvent(false); ThreadPool.QueueUserWorkItem(DoGeocodeLookup, new ParameterRow( m_dtAddress.Rows[index], index, doneEvents[index])); } WaitHandle.WaitAll(doneEvents);
source share