For some reason I am having deadlock when I try to run the SP to insert instant data to the Database
All other threads that using this table are working fine. When i manually add new row to this table through SQL server management studio it release the lock and insert all values that ware in the queue
Here is my code:
Queue<IDbCommand> instatntDataCmdQ;
public void ExecuteInstantDataCmd(int channel, DateTime date, double val, int status)
{
System.Data.SqlClient.SqlCommand cmd = GetInstantDataCmd( channel, date, val, status);
lock (instatntDataCmdQ)
{
try
{
instatntDataCmdQ.Enqueue(cmd);
Monitor.Pulse(instatntDataCmdQ);
}
catch
{
}
}
}
void thInstantDataRun()
{
instatntDataCmdQ = new Queue<IDbCommand>(100);
while (true)
{
try
{
IDbCommand cmd;
lock (instatntDataCmdQ)
{
if (instatntDataCmdQ.Count == 0)
Monitor.Wait(instatntDataCmdQ);
if (instatntDataCmdQ.Count == 0 || (cmd = instatntDataCmdQ.Dequeue()) == null)
break;
}
try
{
//open connection.
if (cmd.Connection.State == ConnectionState.Closed)
cmd.Connection.Open();
if (cmd.Connection.State != ConnectionState.Open)
{
cmd.Connection.Close();
cmd.Connection.Open();
}
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException dex)
{
AddDBlog(cmd.Connection, "thInstantDataRun",
"Critical error, duplicate dateTime&channel in InstantData table\r\n" + dex.Message);
}
catch (Exception err)
{
AddDBlog(cmd.Connection, "thInstantDataRun", "Error Executing Command " + err.Message);
}
finally
{
//do not close connection
//this some time will take up to 1 second and will make the insert of the isntant slow
// cmd.Connection.Close();
cmd.Dispose();
cmd = null;
}
}
catch
{
}
}
}