B
bob
In an effort to keep my UI responsive I have begun to run my time intensive
methods on their own thread and in particular the database inserts.
I open my DB connection in the constructor and close it in the dispose
method. So database_insert method is quick to do its business and finish.
As I was doing these database inserts in response to a timer tick event I
had a situation where the time for my database_insert method was greater
then the tick (especially when I added some time wasting loops to slow it
down).
As I was creating these new threads ( for the database_insert method ) each
tick I can assume that I had more than 1 thread trying to do an insert at a
time. And although the database was getting all the entries I was sending it
AND the UI was still responsive I can't help wondering whether this is a
very very very bad thing to be doing.
1) Should I even be running database operations on a thread I create anyway.
2) Can I check to see if the previous thread has completed. ( considering
that I create it in the tick_event's scope )
3) is the worst that can happen is perhaps my inserts in the database are
out of sequence .. or more probably disaster with a hundred threads all
trying to write at the same time ... ( *shudders* )
I guess I could set a flag that I check before creating a new thread ...
then of course I'd need to consider the thread locking the flag so I don't
read it in a transitional state ... oh dear me ... this is looking knotty.
What should I consider? it seems like a really elegant solution to use
threads do the database operations and keep the UI responsive.
At the moment I'm using the OleDb provider, but I will move to an MySQL one
.... if that has any bearing.
code snippets for those require.
private void timer1_Tick(object sender, System.EventArgs e)
{
update_graphics();
// updates screen & pretty gauges
new Thread( new ThreadStart( DB_insert_method)).Start();
// starts a new thread to handle slower DB calls
}
private void DB_insert_method()
{
// sleep(2000);
oleDbCommand1 = oleDbConnection1.CreateCommand();
oleDbCommand1.CommandText = "INSERT INTO Table1 (myValue,theTime)
VALUES (@myValue, @myTime)";
oleDbCommand1.Parameters.Add ("@myValue",OleDbType.VarChar).Value =
myValue.ToString();
oleDbCommand1.Parameters.Add ("@myTime",OleDbType.Date).Value =
System.DateTime.Now;
oleDbCommand1.CommandType = CommandType.Text;
oleDbCommand1.ExecuteNonQuery();
}
regards Bob
methods on their own thread and in particular the database inserts.
I open my DB connection in the constructor and close it in the dispose
method. So database_insert method is quick to do its business and finish.
As I was doing these database inserts in response to a timer tick event I
had a situation where the time for my database_insert method was greater
then the tick (especially when I added some time wasting loops to slow it
down).
As I was creating these new threads ( for the database_insert method ) each
tick I can assume that I had more than 1 thread trying to do an insert at a
time. And although the database was getting all the entries I was sending it
AND the UI was still responsive I can't help wondering whether this is a
very very very bad thing to be doing.
1) Should I even be running database operations on a thread I create anyway.
2) Can I check to see if the previous thread has completed. ( considering
that I create it in the tick_event's scope )
3) is the worst that can happen is perhaps my inserts in the database are
out of sequence .. or more probably disaster with a hundred threads all
trying to write at the same time ... ( *shudders* )
I guess I could set a flag that I check before creating a new thread ...
then of course I'd need to consider the thread locking the flag so I don't
read it in a transitional state ... oh dear me ... this is looking knotty.
What should I consider? it seems like a really elegant solution to use
threads do the database operations and keep the UI responsive.
At the moment I'm using the OleDb provider, but I will move to an MySQL one
.... if that has any bearing.
code snippets for those require.
private void timer1_Tick(object sender, System.EventArgs e)
{
update_graphics();
// updates screen & pretty gauges
new Thread( new ThreadStart( DB_insert_method)).Start();
// starts a new thread to handle slower DB calls
}
private void DB_insert_method()
{
// sleep(2000);
oleDbCommand1 = oleDbConnection1.CreateCommand();
oleDbCommand1.CommandText = "INSERT INTO Table1 (myValue,theTime)
VALUES (@myValue, @myTime)";
oleDbCommand1.Parameters.Add ("@myValue",OleDbType.VarChar).Value =
myValue.ToString();
oleDbCommand1.Parameters.Add ("@myTime",OleDbType.Date).Value =
System.DateTime.Now;
oleDbCommand1.CommandType = CommandType.Text;
oleDbCommand1.ExecuteNonQuery();
}
regards Bob