I'm trying to import a number of MS Excel (xlsx & xls) files via a multi-threaded process; however I occasionally (approx 5% of the time) receive an error
System.AccessViolationExeption: Attempted to read or write protected memory. This is often an indication that other memory is corrupt
...when multiple oledb or odbc connections are opened simultaneously.
The application appears to work successfully when run on a single thread.
So far i've tried:
- Both OLEDB and ODBC
- Alternate MS Access drivers (Microsoft.ACE.12 and Microsoft..ACE.16)
- Alternate architectures (compiling for x64 & x86)
Code sample below is used to connect to the files;
//// Connecting to file
// Get factory
DbProviderFactory dbfct = DbProviderFactories.GetFactory("System.Data.OleDb");
// Connect to db
DbConnection dbconn = dbfct.CreateConnection();
dbconn.ConnectionString = dataObject.GetSourceConnectionString(filename);
// Attempt to open connection
dbconn.Open();
DbCommand dbcmd = dbconn.CreateCommand();
dbcmd.CommandText = dataObject.GetSourceCommandString(filename);
// Read the data
using (IDataReader sourcerdr = dbcmd.ExecuteReader())
{
// IDataReader is passed to SQL Bulk copy to load into database
ProcessDataFromReader(dataObject, sourcerdr, byPassPrepare);
}
// Close & dispose
dbconn.Close();
dbcmd.Dispose();
//// Code used to create multiple tasks to process across multiple threads
// Create x tasks to process
Task[] consumers = new Task[ProcessConcurrency];
for (int i = 1; i <= ProcessConcurrency; i++)
{
// Log the task ids
string taskId = i.ToString();
consumers[i - 1] = Task.Run(() => ConsumeQueue() // Pass off to an object processor
);
}
// Wait for the processors to complete
await Task.WhenAll(consumers);