AccesViolationExeption was unhandled within (parallel) foreach loop

  • Thread starter Thread starter Matthijs de Z
  • Start date Start date
M

Matthijs de Z

Hi,

I’ve got a problem with a foreach loop. First I thought it had
something to do with parallel.foreach, but it also doesn’t work with a
normal foreach or parallel.foreach with the option
MaxDegreeOfParallelism = 1.

This is the basic code behind the problem:

ParallelOptions options = new ParallelOptions()
{ MaxDegreeOfParallelism = 1 };
Debug.WriteLine("New batch");
Parallel.ForEach(myTickerStringAndIDList, options, item =>
{
Debug.WriteLine("Ticker " + item.Ticker + " TickerID "
+ item.TickerID);
DownloadHandler myDownloadHandler = new
DownloadHandler();
myDownloadHandler.DownloadTickers(item.Ticker.Trim(),
item.TickerID, startDate, endDate, updateTable);
});


The downloadhandler class looks something like this:

public class DownloadHandler
{
private EOdStockQuotesDB db;

public DownloadHandler()
{
db = ConnectionProvider.GetConnection();
}

public void DownloadTickers(string ticker, uint tickerID,
DateTime? startDate, DateTime? endDate, bool updateTable)
{
string urlTemplate = //string is made after some
manipulation of parameters

string EODInfo = String.Empty;

WebClient wc = new WebClient();
try
{
EODInfo = wc.DownloadString(urlTemplate);
//=====================================
//this is the place where it goes wrong
//=====================================
}
catch (WebException wex)
{
// throw wex;
}
finally
{
wc.Dispose();
}

AddEOD2DB(EODInfo, tickerID, true);
}

#region DB handling

private void AddEOD2DB(string EODInfo, uint tickerID, bool
update)
{
EODInfo = EODInfo.Replace("\r", "");

string[] rows = EODInfo.Split('\n');

string[] rowValues;
object[] rowItems;

string query = String.Empty;

for (int i = rows.Length - 1; i > 0; i--)
{
rowValues = rows.Split(',');
if (!String.IsNullOrEmpty(rowValues[0]))
{
DateTime date = Convert.ToDateTime(rowValues[0]);
rowValues[0] = convertDate2DBdate(date);
}

rowItems = ConvertStringArrayToObjectArray(rowValues);
if (rowItems[0] != null && (string)rowItems[0] != "")
{
if (update)
query = String.Format(@"INSERT INTO
`quotes`(`DBDate`,`TickerID`,`Open`,`High`,`Low`,`Close`,`AdjClose`,`Volume`)
values
( '{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')
ON DUPLICATE KEY
UPDATE
`Open`='{2}',`High`='{3}',`Low`='{4}',`Close`='{5}',`AdjClose`='{6}',`Volume`='{7}'",
rowItems[0], tickerID,
rowItems[1], rowItems[2], rowItems[3], rowItems[4], rowItems[6],
rowItems[5]);
else
query = String.Format(@"INSERT IGNORE INTO
`quotes`(`DBDate`,`TickerID`,`Open`,`High`,`Low`,`Close`,`AdjClose`,`Volume`)
values
( '{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')",
rowItems[0], tickerID,
rowItems[1], rowItems[2], rowItems[3], rowItems[4], rowItems[6],
rowItems[5]);

db.ExecuteCommand(query);
//================================================
//sometime I get an error here (hard to replicate)
//================================================
}
}
}



The error message I get within the donwloadticker method is:

AccesViolationExeption was unhandled
Attempted to read or write protected memory. This is often an
indication that other memory is corrupt.

I thought, by using new downloadhandler() withing the foreach, every
request would have it’s own class to get and process the data.

Hope someone can help me out with this problem.
Kind regards,

Matthijs
 
I’ve got a problem with a foreach loop. First I thought it had
something to do with parallel.foreach, but it also doesn’t work with a
normal foreach or parallel.foreach with the option
MaxDegreeOfParallelism = 1.

This is the basic code behind the problem:
The error message I get within the donwloadticker method is:

AccesViolationExeption was unhandled
Attempted to read or write protected memory. This is often an
indication that other memory is corrupt.

I thought, by using new downloadhandler() withing the foreach, every
request would have it’s own class to get and process the data.

Hope someone can help me out with this problem.

Any native code involved?

Arne
 
Back
Top