这是锁定和修改DataTable
由多个线程共享的正确方法吗?如果不是,那么正确的方法是什么?
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();
}
我的结构:
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;
}
}
我开始所有线程的片段:
//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);