I use SQLITE and I want to be able to regularly check the status of the database to display icon on the UI.
As the database will be on a remote share (another computer or a server), I need to be able to perform this check without freezing the application for a minute because the remote host is not reachable.
For that I use the following code:
public bool Connexion_Possible()
{
bool possible = false;
DateTime start_time = DateTime.Now;
Thread t = new Thread
(
new ThreadStart(delegate ()
{
SQLiteConnection sqlite_conn;
sqlite_conn = new SQLiteConnection("Data Source=" + Localisation_BDD() + ";Version=3;FailIfMissing=True;Pooling=False;");
try
{
sqlite_conn.Open();
possible = (sqlite_conn.State == ConnectionState.Open);
sqlite_conn.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Erreur thread", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
sqlite_conn.Dispose();
}
})
);
t.Start();
bool completed = t.Join(500);
MessageBox.Show("Joined after : " + (DateTime.Now - start_time).TotalMilliseconds.ToString("N0"), "", MessageBoxButton.OK, MessageBoxImage.Error);
if (!completed) {t.Interrupt(); t.Abort(); }
MessageBox.Show("Aborted after : " + (DateTime.Now - start_time).TotalMilliseconds.ToString("N0"), "", MessageBoxButton.OK, MessageBoxImage.Error);
return possible;
}
The problem is that the application is frozen for about twenty seconds, it is much longer than the 500ms planned...
MessageBoxs displayed :
Joined after : 496
Aborted after : 20 538
Erreur thread = unable to open database file.
The inability to join the database is normal because I am trying to manage this case.
My problem is : why do I have 20 seconds between the joining and the abortion of the thread?