S
Steve B. [Labo.Net]
Hi
I wrote a little software that call one stored procedure many times.
It works fine using one single thread, but since I use 10 threads to speed
up the process, I get lots of error, such "the connection is closed", "a
datareader is already open to the connection", etc...
Here is a simple view of my code :
private Thread[] _th = new Thread[10];
private void btnGo_Click(object sender, System.EventArgs e)
{
sqlConn.ConnectionString = ssd.ConnectionString;
if (sqlConn.State != ConnectionState.Open)
{
sqlConn.Open();
}
_insertedItems = 0;
_itemsLasting = (int)nudIterations.Value;
_totalItemToInsert = (int)nudIterations.Value;
for(int i=0; i<_th.Length; i++)
{
ThreadStart ts = new ThreadStart(StartCalling);
_th = new Thread(ts);
_th.Start();
}
ticker.Enabled = true;
}
private void StartCalling()
{
while (_insertedItems < _totalItemToInsert)
{
System.Threading.Interlocked.Increment(ref _insertedItems);
System.Threading.Interlocked.Decrement(ref _itemsLasting);
InsertCall();
//_insertedItems++;
//_itemsLasting--;
}
ticker.Enabled = false;
if (sqlConn.State != ConnectionState.Closed)
sqlConn.Close();
}
private Random rnd = new Random();
private void InsertCall()
{
try
{
// creating parameters
int fromNumber;
lock(cmdGetRandomCallNumber)
{
cmdGetRandomCallNumber.ExecuteNonQuery();
fromNumber = (int)cmdGetRandomCallNumber.Parameters["@CallNumber"].Value;
}
int indicatif = rnd.Next(10);
int ville = rnd.Next(1000);
int phone = rnd.Next(10000);
System.Text.StringBuilder sb =new System.Text.StringBuilder(20);
sb.AppendFormat("{0:00} - {1:000} {2:0000}", indicatif, ville, phone);
int duree = rnd.Next(10000);
System.Globalization.Calendar cal = new
System.Globalization.GregorianCalendar();
int month = 1 + rnd.Next(12);
int year = DateTime.Now.Year;
int day = 1 + (rnd.Next(31)%(cal.GetDaysInMonth(year, month)-1));
DateTime dt = new DateTime(year, month, day);
lock(cmdInsertCall)
{
cmdInsertCall.Parameters["@ToNumber"].Value = sb.ToString();
cmdInsertCall.Parameters["@CallDuration"].Value = duree;
cmdInsertCall.Parameters["@FromNumber"].Value = fromNumber;
cmdInsertCall.Parameters["@CallDate"].Value = dt;
// Executing the SP
cmdInsertCall.ExecuteNonQuery();
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
Any Idea ...???
Thanks
I wrote a little software that call one stored procedure many times.
It works fine using one single thread, but since I use 10 threads to speed
up the process, I get lots of error, such "the connection is closed", "a
datareader is already open to the connection", etc...
Here is a simple view of my code :
private Thread[] _th = new Thread[10];
private void btnGo_Click(object sender, System.EventArgs e)
{
sqlConn.ConnectionString = ssd.ConnectionString;
if (sqlConn.State != ConnectionState.Open)
{
sqlConn.Open();
}
_insertedItems = 0;
_itemsLasting = (int)nudIterations.Value;
_totalItemToInsert = (int)nudIterations.Value;
for(int i=0; i<_th.Length; i++)
{
ThreadStart ts = new ThreadStart(StartCalling);
_th = new Thread(ts);
_th.Start();
}
ticker.Enabled = true;
}
private void StartCalling()
{
while (_insertedItems < _totalItemToInsert)
{
System.Threading.Interlocked.Increment(ref _insertedItems);
System.Threading.Interlocked.Decrement(ref _itemsLasting);
InsertCall();
//_insertedItems++;
//_itemsLasting--;
}
ticker.Enabled = false;
if (sqlConn.State != ConnectionState.Closed)
sqlConn.Close();
}
private Random rnd = new Random();
private void InsertCall()
{
try
{
// creating parameters
int fromNumber;
lock(cmdGetRandomCallNumber)
{
cmdGetRandomCallNumber.ExecuteNonQuery();
fromNumber = (int)cmdGetRandomCallNumber.Parameters["@CallNumber"].Value;
}
int indicatif = rnd.Next(10);
int ville = rnd.Next(1000);
int phone = rnd.Next(10000);
System.Text.StringBuilder sb =new System.Text.StringBuilder(20);
sb.AppendFormat("{0:00} - {1:000} {2:0000}", indicatif, ville, phone);
int duree = rnd.Next(10000);
System.Globalization.Calendar cal = new
System.Globalization.GregorianCalendar();
int month = 1 + rnd.Next(12);
int year = DateTime.Now.Year;
int day = 1 + (rnd.Next(31)%(cal.GetDaysInMonth(year, month)-1));
DateTime dt = new DateTime(year, month, day);
lock(cmdInsertCall)
{
cmdInsertCall.Parameters["@ToNumber"].Value = sb.ToString();
cmdInsertCall.Parameters["@CallDuration"].Value = duree;
cmdInsertCall.Parameters["@FromNumber"].Value = fromNumber;
cmdInsertCall.Parameters["@CallDate"].Value = dt;
// Executing the SP
cmdInsertCall.ExecuteNonQuery();
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
Any Idea ...???
Thanks