I have c# project which consists basically of the following 3 classes. It should asynchronously reads a local database (sqlite) and continue with the main process. Though it does not throw any errors, nor executes the db reading. It only executes the infinite loop contained in the main process while(true) {"Doing Stuff on the Main Thread..."}
. It seems the main thread is not waiting for the asynchronous process. Thanks in advance.
class DatabaseAccessor
{
static void Main(string[] args)
{
var inq = new InquireDatabaseAsync();
inq.DoWork();
while (true)
{
Console.WriteLine("Doing Stuff on the Main Thread...");
}
}
}
public class InquireDatabaseAsync
{
public async Task DoWork()
{
await Task.Run(() =>
{
LongRunningOperation();
});
}
private static async Task LongRunningOperation()
{
Data_connection2 dbobject = new Data_connection2();
SQLiteConnection m_dbConnection = new SQLiteConnection();
m_dbConnection.ConnectionString = dbobject.datalocation2();
m_dbConnection.Open();
string sql = "SELECT * FROM Commands WHERE Id = (SELECT MAX(Id) FROM Commands)";
SQLiteCommand SQLcommand = new SQLiteCommand(sql, m_dbConnection);
var instruct = await ExecuteLoopTaskAsync(SQLcommand);
}
public async static Task<string> ExecuteLoopTaskAsync(SQLiteCommand sqlCommand)
{
while (true)
{
System.Threading.Thread.Sleep(1000);
var reader = sqlCommand.ExecuteReader();
while (reader.Read())
Console.WriteLine("Id: " + reader["Id"] + "\tInstruction: " + reader["Instruction"] + "\tCellular: " + reader["Cellular"] + "\tTimestamp: " + reader["Timestamp"]);
}
return "Finished...";
}
}
class Data_connection2
{
public string datalocation2()
{
String dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
return "Data Source=" + dir + "\\database9.sqlite";
}
}