C# 辛塔克斯 基本上考虑事物应该是“一直向下”的范例。这意味着,在任何给定异步调用堆栈的顶层,都应该有一些东西来管理任务。对于WinForms、WPF、ASP.NET等技术,有内置的机制来实现这一点。对于一个控制台应用程序,你基本上是赤裸裸的。main()方法是最顶层的,需要在堆栈上管理它下面的任务。
您可能想做的是C# 辛塔克斯 基本上考虑事物应该是“一直向下”的范例。这意味着,在任何给定异步调用堆栈的顶层,都应该有一些东西来管理任务。对于WinForms、WPF、ASP.NET等技术,有内置的机制来实现这一点。对于一个控制台应用程序,你基本上是赤裸裸的。main()方法是最顶层的,需要在堆栈上管理它下面的任务。,c#,sqlite,asynchronous,async-await,C#,Sqlite,Asynchronous,Async Await,您可能想做的是 static void Main(string[] args) { var enquiry = new InquireDatabaseAsync().DoWork(); while (!enquiry.IsCompleted) { Console.WriteLine("Doing Stuff on the Main Thread..."); } // In case there were any exceptions.
static void Main(string[] args)
{
var enquiry = new InquireDatabaseAsync().DoWork();
while (!enquiry.IsCompleted)
{
Console.WriteLine("Doing Stuff on the Main Thread...");
}
// In case there were any exceptions.
enquiry.Wait();
}
你可能想做的是
static void Main(string[] args)
{
var enquiry = new InquireDatabaseAsync().DoWork();
while (!enquiry.IsCompleted)
{
Console.WriteLine("Doing Stuff on the Main Thread...");
}
// In case there were any exceptions.
enquiry.Wait();
}
你可能想做的是
static void Main(string[] args)
{
var enquiry = new InquireDatabaseAsync().DoWork();
while (!enquiry.IsCompleted)
{
Console.WriteLine("Doing Stuff on the Main Thread...");
}
// In case there were any exceptions.
enquiry.Wait();
}
你可能想做的是
static void Main(string[] args)
{
var enquiry = new InquireDatabaseAsync().DoWork();
while (!enquiry.IsCompleted)
{
Console.WriteLine("Doing Stuff on the Main Thread...");
}
// In case there were any exceptions.
enquiry.Wait();
}
虽然
DoWork
在内部等待任务,但它会立即返回给调用者!因此,如果调用方希望在继续之前等待任务终止,则必须再次等待
inq.DoWork().Wait();
虽然
DoWork
在内部等待任务,但它会立即返回给调用者!因此,如果调用方希望在继续之前等待任务终止,则必须再次等待
inq.DoWork().Wait();
虽然
DoWork
在内部等待任务,但它会立即返回给调用者!因此,如果调用方希望在继续之前等待任务终止,则必须再次等待
inq.DoWork().Wait();
虽然
DoWork
在内部等待任务,但它会立即返回给调用者!因此,如果调用方希望在继续之前等待任务终止,则必须再次等待
inq.DoWork().Wait();
这段代码有几个问题。考虑使用如下的东西:
using System;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class DatabaseAccessor
{
static async void Main(string[] args)
{
await Task.Run(() =>
{
InquireDatabaseAsync.LongRunningOperation();
});
}
}
public static class InquireDatabaseAsync
{
public static void 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);
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"]);
break;
}
Console.WriteLine("Finished.");
}
}
class Data_connection2
{
public string datalocation2()
{
String dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
return "Data Source=" + dir + "\\database9.sqlite";
}
}
}
这段代码有几个问题。考虑使用如下的东西:
using System;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class DatabaseAccessor
{
static async void Main(string[] args)
{
await Task.Run(() =>
{
InquireDatabaseAsync.LongRunningOperation();
});
}
}
public static class InquireDatabaseAsync
{
public static void 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);
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"]);
break;
}
Console.WriteLine("Finished.");
}
}
class Data_connection2
{
public string datalocation2()
{
String dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
return "Data Source=" + dir + "\\database9.sqlite";
}
}
}
这段代码有几个问题。考虑使用如下的东西:
using System;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class DatabaseAccessor
{
static async void Main(string[] args)
{
await Task.Run(() =>
{
InquireDatabaseAsync.LongRunningOperation();
});
}
}
public static class InquireDatabaseAsync
{
public static void 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);
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"]);
break;
}
Console.WriteLine("Finished.");
}
}
class Data_connection2
{
public string datalocation2()
{
String dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
return "Data Source=" + dir + "\\database9.sqlite";
}
}
}
这段代码有几个问题。考虑使用如下的东西:
using System;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class DatabaseAccessor
{
static async void Main(string[] args)
{
await Task.Run(() =>
{
InquireDatabaseAsync.LongRunningOperation();
});
}
}
public static class InquireDatabaseAsync
{
public static void 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);
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"]);
break;
}
Console.WriteLine("Finished.");
}
}
class Data_connection2
{
public string datalocation2()
{
String dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
return "Data Source=" + dir + "\\database9.sqlite";
}
}
}
“看来主线程没有等待异步进程。”为什么会这样?您希望哪段代码会让它等待?“看来主线程没有等待异步进程。”为什么会这样?您希望哪段代码会让它等待?“看来主线程没有等待异步进程。”为什么会这样?您希望哪段代码会让它等待?“看来主线程没有等待异步进程。”为什么会这样?你希望哪一段代码能让它等待?一个人不能在一个本身不是
async
的方法中使用await
。一个人不能在一个本身不是async
的方法中使用await
。一个人不能在一个本身不是async
的方法中使用await
。一个人不能使用await
在一个本身不是async
的方法中。嗨,大卫,谢谢你的老练评论。这实际上解决了我的疑问。嗨,大卫,谢谢你老练的评论。这实际上解决了我的疑问。嗨,大卫,谢谢你老练的评论。这实际上解决了我的疑问。嗨,大卫,谢谢你老练的评论。它实际上解决了我的疑问。对不起,由于SQLite的原因,我无法编译这段代码。我没有意识到不能有一个异步main方法。不管怎么说,这些技术与你想做的事情类似。这是在讨论。抱歉,我无法编译此代码,因为SQLite的东西。我没有意识到不能有一个异步main方法。不管怎么说,这些技术与你想做的事情类似。这是在讨论。抱歉,我无法编译此代码,因为SQLite的东西。我没有意识到不能有一个异步main方法。不管怎么说,这些技术与你想做的事情类似。这是在讨论。抱歉,我无法编译此代码,因为SQLite的东西。我没有意识到不能有一个异步main方法。不管怎么说,这些技术与你想做的事情类似。这一点将在会议上讨论。