Actually I am trying to call my mysqlconnection function with two different function where one function running continuously in background with the help of TASK and another function run on button click. My code gives me an error when both function call mysqlconnection function at a same time.
Here is MYSQL connection function code:
public void executequeryy(CommandType type, string commandtext)
{
try
{
cmd.CommandText = commandtext;
cmd.Connection = PublicClass.conn;
if (type == CommandType.StoredProcedure)
{
cmd.CommandType = CommandType.StoredProcedure;
}
else
{
cmd.CommandType = CommandType.Text;
}
PublicClass.conn.Open();
cmd.ExecuteNonQuery();
PublicClass.conn.Close();
}
catch (Exception ex)
{
PublicClass.conn.Close();
}
}
If anyone have an idea please tell me how can I call this connection method 2 or more times from multiple function at a same time.
Example code which are calling this mysql connection fucntion: continuous run function:
public async void fillalldata()
{
while(true)
{
await Task.Run(() =>
{
OverallData(); //this function fill data in continuous mode in background.
//await Task.Delay(2000);
});
}
}
Here is the another example function which is call by buttonclick:
public void getenvlpdata()
{
DbClass objdata = new DbClass();
objdata.executequeryy(CommandType.Text, "insert into OverallData(SensorID,SampleTime,Temperature,TempUnit,AlarmEnabled,AlarmLevel) values('" + SerialNumber + "','" + SampleTime + "','" + OverallValue + "','" + Unit + "','" + AlarmEnabled + "','" + AlarmLevel + "')");
}