i have a database in a server and it seems like the async method doesn\'t work.
Here is my code:
static async void Example()
{
string connectionS
Avoid using void as return type in async methods. Async void is just meant for event handlers. All other async methods should return Task or Task. Try this:
static async Task Example()
{
string connectionString =
"Server=mydomainname.com;" +
"Port=3306;" +
"Database=scratch;" +
"Uid=Assassinbeast;" +
"Password=mypass123;" +
"AllowUserVariables= true;";
MySql.Data.MySqlClient.MySqlConnection sqConnection = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
await sqConnection.OpenAsync();
Console.Write("Opened. Now Click to close");
Console.ReadLine();
sqConnection.Close();
}
static void Main(string[] args)
{
Console.ReadLine();
Task.Run(() => Example()).Wait();
Console.WriteLine("Done");
Console.ReadLine();
}
UPDATE
As explained by dcastro, this is how async-await works:
If the awaited task hasn't finished and is still running, Example() will return to its calling method, thus the main thread doesn't get blocked. When the task is done then a thread from the ThreadPool (can be any thread) will return to Example() at its previous state and continue execution.
Or A second case would be that the task has already finished its execution and the result is available. When reaching the awaited task the compiler knows that it has the result and will keep on executing code on the very same thread.