1

Good day everyone. I've been using the MySQL API to interact with my MySQL database. The only problem is that the asynchronous methods aren't working.

Example of my code:

private MySqlConnection mySqlConnection;

private async Task OpenConnection() {
    //Assign mySqlConnection if null
    //Check if mySqlConnection is closed
    await mySqlConnection.OpenAsync();
}

private async Task<bool> ExampleMethod() {
    try {
        await OpenConnection();
        return true;
    } catch {
        return false;
    }
}

Whenever I execute ExampleMethod my application pauses until the method has completed its operation. Could someone tell me what could be the problem?

Thanks in advance.

2 Answers 2

2

This happens because the Async methods in the MySql.Data connector aren't actually asynchronous. They block on network I/O and only return when the DB operation is complete. (For a much more detailed description, see this question and its top answer.) MySQL bug #70111 reports this problem in the MySQL connector.

To get truly asynchronous DB operations, you'll have to wait until that bug is fixed, or switch to a different connector. I've been developing a new, fully async connector; to try it out, install MySqlConnector from NuGet; its source is at GitHub.

Sign up to request clarification or add additional context in comments.

Comments

0

The debugger will wait for async operations to complete, and that is by design.

async is primarily a tool to let you execute long-running tasks without blocking the UI, and without having to resort to using the traditional thread approaches.

The debugger will block, because there isn't any other running code to switch to.

1 Comment

What do you mean by there isn't any other code to switch to? And also I've tried running the application without debugging and the UI still freezes until the async operation is complete...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.